C#如何获取机器上正在运行的进程?

来源:互联网 发布:黑马程序员教学视频 编辑:程序博客网 时间:2024/05/19 04:50

使用System.Diagnostics.Process类可以启动和停止系统进程。GetProcesses函数返回了机器上正在运行的所有进程,它有两种定义,一种是用于本地机器,另一种用于远程机器。

 

如果想要获取本地机器上的运行进程,使用GetProcesses();否则使用GetProcesses(stringmachinename)

public static Process[] GetProcesses();

public static Process[] GetProcesses(string);

比如:

Process[] procList = new Process[100];

procList = Process.GetProcesses();

for ( int i=0; i<20; i++)

{

string strProcName = procList[i].ProcessName;

int iProcID = procList[i].Id;;

}

请不要忘记引用System.Diagnostic,将以下这行代码写在using列表中:

using System.Diagnostics;

通过使用Process类的方法,我们甚至可以启动、停止或取消进程。