C#实现进程内存信息获取-创新互联

usingSystem.Collections.Generic;
usingSystem.Runtime.InteropServices;
usingSystem;
usingSystem.Diagnostics;
staticclassNat
{
   [StructLayout(LayoutKind.Sequential]
   struct IO_COUNTERS
   {
       publiculongReadOperationCount;
       publiculongWriteOperationCount;
       publiculongOtherOperationCount;
       publiculongReadTransferCount;
       publiculongWriteTransferCount;
       publiculongOtherTransferCount;
   }
   [DllImport("kernel32.dll")]
   unsafestaticextern boolGetProcessIoCounters(IntPtrProcessHandle,out IO_COUNTERSIoCounters);

   [StructLayout(LayoutKind.Sequential,Size=40)]
   privatestruct PROCESS_MEMORY_COUNTERS
   {
       publicuint cb;
       publicuintPageFaultCount;
       publicuintPeakWorkingSetSize;
       publicuintWorkingSetSize;
       publicuintQuotaPeakPagedPoolUsage;
       publicuintQuotaPagedPoolUsage;
       publicuintQuotaPeakNonPagedPoolUsage;
       publicuintQuotaNonPagedPoolUsage;
       publicuintPagefileUsage;
       publicuintPeakPagefileUsage;
   }

   [DllImport("psapi.dll",SetLastError=true)]
   unsafestaticextern boolGetProcessMemoryInfo(IntPtr* hProcess,out PROCESS_MEMORY_COUNTERS*Memcounters,int size);

   publicstaticclass IO
   {
       unsafepublicstaticDictionaryGetALLIO(Process procToRtrivIO)
       {
           IO_COUNTERS counters;
           Dictionary retCountIoDict=newDictionary();
           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
   {
       unsafepublicstaticDictionaryGetAllMem(Process procToRtrivMem)
       {

           PROCESS_MEMORY_COUNTERS*MemCounters;
           Dictionary retCountMemDict=newDictionary();
           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.";

       }
   }
}
C#实现进程内存信息获取

 参考:using unsafe code in C# asp.net  http://stackoverflow.com/questions/17207310/using-unsafe-code-in-c-sharp-asp-net

创新互联公司从2013年成立,是专业互联网技术服务公司,拥有项目网站设计制作、成都网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元塔什库尔干塔吉克做网站,已为上家服务,为塔什库尔干塔吉克各地企业和个人服务,联系电话:18980820575

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


本文题目:C#实现进程内存信息获取-创新互联
当前链接:http://azwzsj.com/article/cddpdp.html