如何在asp.net中获取CPU使用率

Has*_*anG 8 c# asp.net performance cpu-usage

有没有办法在asp.net页面上显示CPU和RAM使用情况统计信息.我试过这段代码,但是我有错误:

Access to the registry key 'Global' is denied.
Run Code Online (Sandbox Code Playgroud)

在这条线上:

ramCounter = new PerformanceCounter("Memory", "Available MBytes");
Run Code Online (Sandbox Code Playgroud)

squ*_*man 5

如评论中所述,如果没有相应的权限,您将无法执行此操作.资源统计信息将包含许多有关系统其他用户拥有的进程的信息,这些信息是特权信息


ukh*_*rdy 5

使用:

    System.Diagnostics.PerformanceCounter cpuUsage = 
      new System.Diagnostics.PerformanceCounter();
    cpuUsage.CategoryName = "Processor"; 
    cpuUsage.CounterName = "% Processor Time";
    cpuUsage.InstanceName = "_Total";

    float f = cpuUsage.NextValue();
Run Code Online (Sandbox Code Playgroud)

编辑:

Windows将性能计数器的访问权限限制为管理员或性能日志用户(在Vista +中)组中的访问权限.设置注册表安全性无法解决此问题.有可能在某处附加了用户权限.