C#进程管理器
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
namespace Diagnostics
{
public partial class MultiProcess : Form
{
public MultiProcess()
{
InitializeComponent();
}
private void buttonStart_Click(object sender, EventArgs e)
{
//启动记事本
process.StartInfo.FileName = "Notepad.exe";
process.Start();
}
private void buttonStop_Click(object sender, EventArgs e)
{
Process[] myProcess;
//创建新的Process组件数组,并将他们与指定的进程名称的所有进程资源相关联
myProcess = Process.GetProcessesByName("Notepad");
foreach(Process instance in myProcess)
{
//设置终止当前线程前等待的毫秒数
instance.WaitForExit(100);
instance.CloseMainWindow();
}
}
//获取所有进程
private void buttonView_Click(object sender, EventArgs e)
{
listViewProcess.Items.Clear();
Process[] allProcess;
//创建Process类型的数组,将他们与系统内所有进程相关联
try
{
allProcess = Process.GetProcesses();
foreach (Process p in allProcess)
{
ListViewItem item = new ListViewItem();
item.SubItems[0].Text = p.ProcessName.ToString();
item.SubItems.Add(p.StartTime.ToShortTimeString());
item.SubItems.Add(p.Id.ToString());
item.SubItems.Add(p.Threads.Count.ToString());
listViewProcess.Items.Add(item);
}
}
catch (System.Exception ee)
{
string a = ee.ToString();
}
}
//结束进程
private void buttonKill_Click(object sender, EventArgs e)
{
ListView.SelectedListViewItemCollection selectItem = listViewProcess.SelectedItems;
foreach (ListViewItem item in selectItem)
{
Process KillProcess = Process.GetProcessById(Convert.ToInt32(item.SubItems[2].Text));
try
{
KillProcess.Kill();
MessageBox.Show("结束进程成功");
buttonView_Click(sender,e);
}
catch (System.Exception ee)
{
string strInfo = ee.ToString();
MessageBox.Show("结束进程失败");
}
}
}
}
}
/Files/eping/Diagnostics.rar