using System.Collections.Generic;
using System.Runtime.InteropServices;
using System;
using System.Diagnostics;
staticclassNat
{
[StructLayout(LayoutKind.Sequential]
struct IO_COUNTERS
{
public ulong ReadOperationCount;
public ulong WriteOperationCount;
public ulong OtherOperationCount;
public ulong ReadTransferCount;
public ulong WriteTransferCount;
public ulong OtherTransferCount;
}
[DllImport("kernel32.dll")]
unsafe static extern bool GetProcessIoCounters(IntPtrProcessHandle,out IO_COUNTERS IoCounters);
[StructLayout(LayoutKind.Sequential,Size=40)]
privatestruct PROCESS_MEMORY_COUNTERS
{
public uint cb;
public uint PageFaultCount;
public uint PeakWorkingSetSize;
public uint WorkingSetSize;
public uint QuotaPeakPagedPoolUsage;
public uint QuotaPagedPoolUsage;
public uint QuotaPeakNonPagedPoolUsage;
public uint QuotaNonPagedPoolUsage;
public uint PagefileUsage;
public uint PeakPagefileUsage;
}
[DllImport("psapi.dll",SetLastError=true)]
unsafe static extern bool GetProcessMemoryInfo(IntPtr* hProcess,out PROCESS_MEMORY_COUNTERS*Memcounters,int size);
publicstaticclass IO
{
unsafepublicstaticDictionary<string,ulong>GetALLIO(Process procToRtrivIO)
{
IO_COUNTERS counters;
Dictionary<string,ulong> retCountIoDict =newDictionary<string,ulong>();
IntPtr ptr =System.Diagnostics.Process.GetCurrentProcess().Handle;
GetProcessIoCounters(ptr,out counters);
retCountIoDict.Add("ReadOperationCount", counters.ReadOperationCount);
retCountIoDict.Add("WriteOperationCount", counters.WriteOperationCount);
retCountIoDict.Add("OtherOperationCount", counters.OtherOperationCount);
retCountIoDict.Add("ReadTransferCount", counters.ReadTransferCount);
retCountIoDict.Add("WriteTransferCount", counters.WriteTransferCount);
retCountIoDict.Add("OtherTransferCount", counters.OtherTransferCount);
return retCountIoDict;
//return "This process has read " + ((counters.ReadTransferCount/1024)/1024).ToString("N0") +
// " Mb of data.";
}
}
publicstaticclassMem
{
unsafe public staticDictionary<string,uint>GetAllMem(Process procToRtrivMem)
{
PROCESS_MEMORY_COUNTERS*MemCounters;
Dictionary<string,uint> retCountMemDict =newDictionary<string,uint>();
IntPtr ptr =System.Diagnostics.Process.GetCurrentProcess().Handle;
GetProcessMemoryInfo(&ptr,outMemCounters,Marshal.SizeOf(typeof(PROCESS_MEMORY_COUNTERS)));//MemCounters.cb);
retCountMemDict.Add("cb",MemCounters->cb);
retCountMemDict.Add("PageFaultCount",MemCounters->PageFaultCount);
retCountMemDict.Add("PeakWorkingSetSize",MemCounters->PeakWorkingSetSize);
retCountMemDict.Add("WorkingSetSize",MemCounters->WorkingSetSize);
retCountMemDict.Add("QuotaPeakPagedPoolUsage",MemCounters->QuotaPeakPagedPoolUsage);
retCountMemDict.Add("QuotaPagedPoolUsage",MemCounters->QuotaPagedPoolUsage);
retCountMemDict.Add("QuotaPeakNonPagedPoolUsage",MemCounters->QuotaPeakNonPagedPoolUsage);
retCountMemDict.Add("QuotaNonPagedPoolUsage",MemCounters->QuotaNonPagedPoolUsage);
retCountMemDict.Add("PagefileUsage",MemCounters->PagefileUsage);
retCountMemDict.Add("PeakPagefileUsage",MemCounters->PeakPagefileUsage);
return retCountMemDict;
//return "This process has read " + ((counters.ReadTransferCount/1024)/1024).ToString("N0") +
// " Mb of data.";
}
}
}
参考:using unsafe code in C# asp.net http://stackoverflow.com/questions/17207310/using-unsafe-code-in-c-sharp-asp-net
collectiong memory usage information for a processhttp://msdn.microsoft.com/en-us/library/windows/desktop/ms682050(v=vs.85).aspx
在C#中调用psapi.dll内置的GetProcessMemoryInfo函数http://social.microsoft.com/Forums/it-IT/650197e0-a21a-4f5e-a974-23f074f52a55/cpsapidllgetprocessmemoryinfo?forum=visualcshartzhchs
ASP.NET(C#)获取当前计算机CPU内存使用率等相关信息http://luzinwbing.blog.163.com/blog/static/113805840201031093415658/
不安全代码只会在使用/unsafe编译情况下使用 http://lixiaorong223.blog.163.com/blog/static/44011629200993181241924/
wmi获得进程的虚拟内存与任务管理器中显示的不一致 http://bbs.csdn.net/topics/260033107