在.NET Core中,如何获取给定进程的CPU使用率和虚拟内存?
Google 搜索结果显示 PerformanceCounter 和 DriverInfo 类可以完成这项工作。但是,PerformanceCounter 和 DriverInfo 类在 .NET Core 中不可用。
stackoverflow上有一篇关于这个问题的帖子:How to get the current CPU/RAM/Disk usage in a C# web application using .NET CORE?
但是它只解决: -当前进程的CPU使用率:
var proc = Process.GetCurrentProcess();
我已获得进程(具有 ProcessID 整数格式)。如何获取 .NET Core 中特定进程的 CPU 使用率和虚拟内存?
最佳答案
您可以在System.Diagnostics.PerformanceCounter
package中使用PerformnceCounter
例如,下一个代码将为您提供总处理器使用百分比
var cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total", true);
var value = cpuCounter.NextValue();
// In most cases you need to call .NextValue() twice
if (Math.Abs(value) <= 0.00)
value = cpuCounter.NextValue();
Console.WriteLine(value);
关于c# - 如何获取.Net Core中进程的CPU使用率和虚拟内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54178660/