使用C#(性能计数器)的远程机器的CPU使用率

时间:2014-03-03 12:48:48

标签: c# performancecounter

我正在尝试使用PerformanceCounter类查找另一台远程计算机的CPU使用情况,但它会抛出异常:

  

“System.ComponentModel.Win32Exception:找不到网络路径   在System.Diagnostics.PerformanceMonitor.Init()“

如果我在Performance Counter Object的Machinename属性中设置我的机器名称,它工作正常。

我已在远程计算机上设置了防火墙设置。

这是代码:

代码如下:

public partial class Default10:System.Web.UI.Page {

private PerformanceCounter cpucounter;
private PerformanceCounter ramcounter;
protected void Page_Load(object sender, EventArgs e)
{

     try{
    InitialiseCPUCounter();
    InitialiseRAMCounter();
    //cpucounter.NextValue();


    cpucounter.MachineName = "XXXXXXXXX";/*Remote Machine Name*/

    cpucounter.NextValue();
    System.Threading.Thread.Sleep(1000);

   this.Label1.Text = "CPU Usage:" + Convert.ToInt32(cpucounter.NextValue()).ToString() + "%"+"<br/>";
   this.Label2.Text ="RAM Usage: " +Convert.ToInt32(ramcounter.NextValue()).ToString() + "MB<br/>";
     }
    catch(Exception ex)
     {
        Response.Write(ex);
    }
  }
  private void InitialiseCPUCounter()
  {
      cpucounter = new PerformanceCounter("Processor", "% Processor Time", "_Total", true);
  }
  private void InitialiseRAMCounter()
  {
      ramcounter = new PerformanceCounter("Memory", "Available MBytes", true);
  }

  }

1 个答案:

答案 0 :(得分:-3)

使用WIN32_Processor(WMI)类的属性LoadPercentage

完成了它