views:

3215

answers:

3

How can i retrieve the current CPU usage in c# using WMI? I've seen plenty of posts using performance counters, but I need a solution that can work with remote machines. I've also found a VB solution here, but I'd prefer to accomplish this in C# if possible.

+1  A: 

the tool "pslist" from sysinternals (its free) could readout the usage from remotemachines. y could start the programm within a cmd-box and fetch the result into your application.

        cmd.StartInfo.FileName = "cmd.com"; 
        cmd.StartInfo.Arguments = "/c pslist ....."; 
        cmd.StartInfo.RedirectStandardOutput = true; 
        cmd.StartInfo.RedirectStandardError  = true; 

        // run process and catch output 
        cmd.Start(); 
        string sOutput = cmd.StandardOutput.ReadToEnd(); 
        cmd.WaitForExit();
DrFuture
+2  A: 

Performance with WMI is messy, to say the least. Performance counters work OK with remote machines. Use the System.Diagnostics.PerformanceCounterXxx classes, the constructors have overloads which take a machineName argument.

Anton Tykhyy
Thx! Didn't realize it took machine names. Went back, tried this way, started seeing this exception 'The network path was not found'.Found this article that may explain it, but by that time, I had already sorted out the WMI mehtod.http://blogs.msdn.com/bclteam/archive/2006/09/08/746900.aspx
JPero
You don't need (and often can't) edit registry on remote machines. Just impersonate a remote user who can read performance data. BTW, access to WMI usually requires administrative privileges too.
Anton Tykhyy
+1  A: 

Got it working. Used pretty much the same code as found here: http://www.csharphelp.com/archives2/archive334.html Turns out I had a bad path, which i finally got sorted out: new ManagementPath(string.Format("\\{0}\root\cimv2",machineName));

JPero