Is there a way to show CPU and RAM usage statistics on an asp.net page. I've tried this code but I have error:
Access to the registry key 'Global' is denied.
on this line:
ramCounter = new PerformanceCounter("Memory", "Available MBytes");
To get the overall CPU usage, get all processes with Process. GetProcesses() and compute the sum of the processor times.
The CPU usage information is easily accessible in every operating system. In Windows, all you have to do is open the Task Manager. Beneath the “Performance” tab, you'll be able to check how much of the CPU is being utilized at the present moment.
To bring up the window again, click Debug > Windows > Show Diagnostic Tools. You can choose whether to see CPU Usage, Memory Usage, or both, with the Select Tools setting on the toolbar. If you are running Visual Studio Enterprise, you can also enable or disable IntelliTrace in Tools > Options > IntelliTrace.
As mentioned in comments, without the appropriate permissions you will not be able to do this. Resource statistics will include a lot of information about processes owned by other users of the system which is privileged information
Use:
System.Diagnostics.PerformanceCounter cpuUsage =
new System.Diagnostics.PerformanceCounter();
cpuUsage.CategoryName = "Processor";
cpuUsage.CounterName = "% Processor Time";
cpuUsage.InstanceName = "_Total";
float f = cpuUsage.NextValue();
Edit:
Windows limits access to the performance counters to those in the Administrators or Performance Logs Users (in Vista+) groups. Setting registry security won't resolve this. It is possible that there is a user right attached to it somewhere.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With