获取进程信息
(1)获取本地计算机的所有进程:
Process[] myProcesses = Process.GetProcesses();
(2)获取本地计算机上指定名称的进程:
Process[] myProcesses =
Process.GetProcessesByName("进程名称");
注意:(a)进程名称不带扩展名。(b)可以是任何一个可执行文件
例如:
Process[] myProcesses = Process.GetProcessesByName (“WindowApplication1");
(3)获取远程计算机的所有进程:
Process[] myProcesses =
Process.GetProcesses (remoteMachineName);
例如:
Process[] myProcesses = Process.GetProcesses ("192.168.0.1");
(4)获取远程计算机上指定名称的进程:
Process[] myProcesses = Process.GetProcessesByName( "远程进程名称",remoteMachineName);
本机运行的所有进程,并显示进程相关的信息。
效果如下
示例代码
Process[] myProcess;
//得到多有进程
private void GetAllProcess()
{
dataGridView1.Rows.Clear();
myProcess = Process.GetProcesses();
foreach (Process p in myProcess)
{
int newRowIndex = dataGridView1.Rows.Add();
DataGridViewRow row = dataGridView1.Rows[newRowIndex];
row.Cells[0].Value = p.Id;
row.Cells[1].Value = p.ProcessName;
row.Cells[2].Value = string.Format("{0:###,##0.00}MB", p.WorkingSet64 / 1024.0f / 1024.0f);
//有些进程无法获取启动时间和文件名信息,所以要用try/catch
try
{
row.Cells[3].Value = string.Format("{0}", p.StartTime);
row.Cells[4].Value = p.MainModule.FileName;
}
catch
{
row.Cells[3].Value = "";
row.Cells[4].Value = "";
}
}
}
//查看进程信息
private void ShowProcessInfo(Process p)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("进程名称:" + p.ProcessName + ", ID:" + p.Id);
try
{
sb.AppendLine("进程优先级:" + p.BasePriority + "(优先级类别: " + p.PriorityClass + ")");
ProcessModule m = p.MainModule;
sb.AppendLine("文件名:" + m.FileName);
sb.AppendLine("版本:" + m.FileVersionInfo.FileVersion);
sb.AppendLine("描述:" + m.FileVersionInfo.FileDescription);
sb.AppendLine("语言:" + m.FileVersionInfo.Language);
sb.AppendLine("------------------------");
if (p.Modules != null)
{
ProcessModuleCollection pmc = p.Modules;
sb.AppendLine("调用的模块(.dll):");
for (int i = 1; i < pmc.Count; i++)
{
sb.AppendLine(
"模块名:" + pmc[i].ModuleName + "\t" +
"版本:" + pmc[i].FileVersionInfo.FileVersion + "\t" +
"描述:" + pmc[i].FileVersionInfo.FileDescription);
}
}
}
catch
{
sb.AppendLine("其他信息:无法获取");
}
this.richTextBox1.Text = sb.ToString();
}