I want to get the RAM (in MB) and CPU (%) using of my program in C# but I don't know how.
-
Although asked for C++, you can find some useful information here: http://stackoverflow.com/questions/18328129/detect-hardware-information-in-c-application – Cool_Coder Aug 30 '14 at 12:46
-
And for the CPU time part of the question, see answers here: http://stackoverflow.com/questions/9501645/performance-counter-cpu-usage-for-current-process-is-more-than-100 – Anders Forsgren Aug 30 '14 at 12:53
2 Answers
You can try to use PerformanceCounter Class:-
private PerformanceCounter theCPUCounter =
new PerformanceCounter("Process", "% Processor Time",
Process.GetCurrentProcess().ProcessName);
You may need to add this to use the above:-
using System.Diagnostics;
Also check How to get the CPU Usage in C#? and Process Properties and this MSDN forum for CPU usage

- 1
- 1

- 168,305
- 31
- 280
- 331
For ram it's pretty easy you can be use variables of the environment static class.
One such variable is Environment.WorkingSet, i'll let you look up MSDN for the others and their differences.
For cpu it's more complex because cpu usage of a program varies a lot over time unlike ram that only does when you expect it (programs get scheduled / priorized by Windows, could be 100% cpu usage during a few miliseconds, 0% next few miliseconds, if you checked it you'd see either 0 to 100 while you're looking for an average).
You'll have to query it (using WMI i assume) and average it every second or so to get something interesting.

- 3,413
- 3
- 29
- 78