如何使用.NET CORE在C#Web应用程序中获取当前的CPU / RAM /磁盘使用情况?

我目前正在寻找一种方法来使用.NET CORE在C#Web应用程序中获取当前的CPU / RAM /磁盘使用情况。

对于CPU和RAM使用,我使用System.Diagnostics中的PerformanceCounter类。 这些是代码:

PerformanceCounter cpuCounter; PerformanceCounter ramCounter; cpuCounter = new PerformanceCounter(); cpuCounter.CategoryName = "Processor"; cpuCounter.CounterName = "% Processor Time"; cpuCounter.InstanceName = "_Total"; ramCounter = new PerformanceCounter("Memory", "Available MBytes"); public string getCurrentCpuUsage(){ cpuCounter.NextValue()+"%"; } public string getAvailableRAM(){ ramCounter.NextValue()+"MB"; } 

对于磁盘使用,我使用DriveInfo类。 这些是代码:

  using System; using System.IO; class Info { public static void Main() { DriveInfo[] drives = DriveInfo.GetDrives(); foreach (DriveInfo drive in drives) { //There are more attributes you can use. //Check the MSDN link for a complete example. Console.WriteLine(drive.Name); if (drive.IsReady) Console.WriteLine(drive.TotalSize); } } } 

不幸的是,.NET Core不支持DriveInfo和PerformanceCounter类,因此上面的代码不起作用。

有谁知道如何使用.NET CORE在C#Web应用程序中获取当前的CPU / RAM /磁盘使用情况?

处理器信息可通过System.Diagnostics

 var proc = Process.GetCurrentProcess(); var mem = proc.WorkingSet64; var cpu = proc.TotalProcessorTime; Console.WriteLine("My process used working set {0:n3} K of working set and CPU {1:n} msec", mem / 1024.0, cpu.TotalMilliseconds); 

通过添加System.IO.FileSystem.DriveInfo包,可以为Core提供DriveInfo