.NET ez 2012-06-04
使用 "效能計數器" 取得 CPU 使用率,但此方法如果有相同名稱,就無法取得所要項目的使用率。
程式碼如下:
using System; using System.Diagnostics; namespace Test { class Program { static void Main(string[] args) { UsingProcess("OUTLOOK"); } static void UsingProcess(string pname) { using (var pro = Process.GetProcessesByName(pname)[0]) { PerformanceCounter pf = new PerformanceCounter(); //性能計數器 pf.CategoryName = "Process"; pf.CounterName = "% Processor Time"; pf.InstanceName = pro.ProcessName; pf.MachineName = "."; while (true) { Console.WriteLine("RAM:" + (Convert.ToInt64(pro.WorkingSet64.ToString()) / 1024).ToString() + " CPU:" + Math.Round(pf.NextValue(), 2).ToString() + "%"); System.Threading.Thread.Sleep(1000); } } } } }
標籤: .NET
本文章網址:
https://www.ez2o.com/Blog/Post/csharp-Get-Process-PerformanceCounter
https://www.ez2o.com/Blog/Post/169
https://www.ez2o.com/Blog/Post/csharp-Get-Process-PerformanceCounter
https://www.ez2o.com/Blog/Post/169