如何在C#应用程序中获取有关进程(cpu,内存,磁盘和网络用法)的信息?
P.S。 System.Diagnostics.Process和System.Diagnostics.PerformanceCounter不提供有关磁盘和网络使用情况的信息。我不使用它。
答案 0 :(得分:2)
System.Text.StringBuilder sb = new System.Text.StringBuilder();
var currentProcess = System.Diagnostics.Process.GetCurrentProcess();
sb.AppendLine("Process information");
sb.AppendLine("-------------------");
sb.AppendLine("CPU time");
sb.AppendLine(string.Format("\tTotal {0}",
currentProcess.TotalProcessorTime));
sb.AppendLine(string.Format("\tUser {0}",
currentProcess.UserProcessorTime));
sb.AppendLine(string.Format("\tPrivileged {0}",
currentProcess.PrivilegedProcessorTime));
sb.AppendLine("Memory usage");
sb.AppendLine(string.Format("\tCurrent {0:N0} B", currentProcess.WorkingSet64));
sb.AppendLine(string.Format("\tPeak {0:N0} B", currentProcess.PeakWorkingSet64));
sb.AppendLine(string.Format("Active threads {0:N0}", currentProcess.Threads.Count));
等
答案 1 :(得分:1)
作为首发,这将获得所有进程的列表并循环遍历它:
using System.Diagnostics;
//...
Process[] all = Process.GetProcesses();
foreach (Process thisProc in all) {
string Name = thisProc.ProcessName;
//...
}
答案 2 :(得分:0)
将此行添加到您的使用列表中:
using System.Diagnostics;
现在,您可以使用Process.GetProcesses()
方法获取流程列表,如下例所示:
Process[] processlist = Process.GetProcesses();
foreach(Process theprocess in processlist)
{
Console.WriteLine(“Process: {0} ID: {1}”, theprocess.ProcessName, theprocess.Id);
}
答案 3 :(得分:0)
在System.Diagnostics中有一个名为Process的类,它允许您按名称,ID等静态获取Processess。所以你可以做类似
的事情Process p = Process.GetCurrentProcess();
int pageMemSize = p.PagedMemorySize;
该课程有很多不同的属性。查看Process类here