Question

I am trying to find CPU usage of another remote machine using PerformanceCounter class but it is throwing an exception:

"System.ComponentModel.Win32Exception: The network path was not found at System.Diagnostics.PerformanceMonitor.Init() "

It is working fine if I set my machine name in Performance Counter Object's Machinename Property.

I have set Firewall setings off on remote machine .

Here is Code:

Code is as:

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);
  }

  }
Was it helpful?

Solution

Got it done using WIN32_Processor(WMI) class's property LoadPercentage

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top