2012-03-28 81 views
-2

您如何在c sharp中使用「性能計數器」獲取總內存,CPU使用率等WMI信息。獲取WMI信息的C代碼

+1

以前問的問題... – 2012-03-28 09:08:58

+0

嘗試http://www.codeproject.com/Articles/26310/Using-WMI-to-retrieve-processor-in-in-C – 2012-03-28 09:10:13

+0

所以你能幫忙嗎? – user1297661 2012-03-28 09:10:43

回答

1
private static PerformanceCounter cpuCounter; 
private static PerformanceCounter memoryCounter; 

[...] 

cpuCounter = new PerformanceCounter 
{ 
    CategoryName = "Processor", 
    CounterName = "% Processor Time", 
    InstanceName = "_Total" 
}; 


memoryCounter = new PerformanceCounter 
{ 
    CategoryName = "Memory", 
    CounterName = "Available Bytes" 
}; 

[...] 

public double CpuUsage 
{ 
    get 
    { 
     lock (lockToken) 
     { 
      return Math.Round(cpuCounter.NextValue(), 2); 
     } 
    } 
} 

public double MemoryUsage 
{ 
    get 
    { 
     lock (lockToken) 
     { 
      return Math.Round(memoryCounter.NextValue()/totalMemory * 100, 2); 
     } 
    } 
    } 

總內存:

using (var searcher = new ManagementObjectSearcher("SELECT totalphysicalmemory FROM Win32_ComputerSystem")) 
     { 
      using (var wmiData = searcher.Get()) 
      { 
       foreach (var mo in wmiData) 
       { 
        totalMemory = long.Parse(mo["totalphysicalmemory"].ToString()); 
       } 
      } 
     } 

爲了讓你必須自己做一些研究的虛擬內存。

+0

謝謝你。你如何獲取可用內存和虛擬內存。 – user1297661 2012-03-28 09:17:18

+0

@ user1297661,看我的編輯。 – lurkerbelow 2012-03-28 09:21:16

+0

感謝幫助 – user1297661 2012-03-28 09:36:07