在Windows下,可以使用以下方法读取.NET性能计数器:
PerformanceCounter考虑到最近发布的.NET Core以及此类应用程序也可以在Linux中运行的事实,如何访问Windows上当前可用的.NET相关统计数据?
我目前正在寻找一种使用.NET CORE在C#Web应用程序中获取当前CPU / RAM /磁盘使用情况的方法。
对于CPU和内存的使用,我使用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";
}
Run Code Online (Sandbox Code Playgroud)
对于磁盘使用,我使用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); …Run Code Online (Sandbox Code Playgroud)