c# - 如何获取.Net Core中进程的CPU使用率和虚拟内存?

标签 c# .net-core

在.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/

相关文章:

c# - WPF如何更改首先打开的窗口

c# - 使用 Entity Framework 代码优先创建表、运行时

.net - EF Core 使用类库项目和 DB 脚手架从用户 secret 中读取连接字符串

c# - 什么是 ASP.NET Core 中的服务器垃圾回收?

c# - 使用 C# WebClient 伪造表单提交

c# - mscorlib 5.0.5.0 中缺少 System.Security.Cryptography.RSA

c# - 返回在 'using' 语句中创建的实例

.net-core - Microsoft.NETCore.ILAsm 中的 ilasm.exe 在哪里?

c# - ERR_CONNECTION_REFUSED - .Net Core 1.0.1

azure - 使用 Azure MSI 访问 Azure 表存储