簡體   English   中英

使用C#(性能計數器)的遠程計算機的CPU使用率

[英]CPU usage of remote Machine using C#(Performance Counter)

我正在嘗試使用PerformanceCounter類查找另一台遠程計算機的CPU使用率,但它拋出異常:

“ System.ComponentModel.Win32Exception:在System.Diagnostics.PerformanceMonitor.Init()處找不到網絡路徑”

如果我在Performance Counter Object的Machinename屬性中設置機器名稱,則工作正常。

我已經在遠程計算機上關閉了防火牆設置。

這是代碼:

代碼如下:

公共局部類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);
  }

  }

使用WIN32_Processor(WMI)類的屬性LoadPercentage完成此操作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM