Process用于管理计算机的进程,下面给出一个C#进程管理器的DEMO。
namespace ProcessManager { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { listView1.FullRowSelect = true; GetProcess(); } private void GetProcess() { listView1.Items.Clear(); Process[] proList = Process.GetProcesses(".");//获得本机的进程 lblNumber.Text = proList.Length.ToString(); //当前进程数量 foreach (Process p in proList) { ListViewItem lvi = new ListViewItem(); lvi.Text = p.ProcessName; lvi.SubItems.AddRange(new string[] { p.Id.ToString(), p.PrivateMemorySize64.ToString() }); //进程ID 使用内存 listView1.Items.Add(lvi); } } private void btnStop_Click(object sender, EventArgs e) { if (listView1.SelectedItems.Count > 0) { try { string proName = listView1.SelectedItems[0].Text; Process[] p = Process.GetProcessesByName(proName); //根据进程命获得指定的进程 p[0].Kill(); //杀死该进程 MessageBox.Show("进程关闭成功!"); GetProcess(); } catch { MessageBox.Show("无法关闭此进程!"); } } else { MessageBox.Show("请选择要终止的进程!"); } } private void btnStart_Click(object sender, EventArgs e) { if (txtProcess.Text != string.Empty) { Process.Start(txtProcess.Text); //启动一个进程 } else { MessageBox.Show("请输入启动项"); txtProcess.Focus(); } } } }
显示如下: