先修改注册表:
HKEY_LOCAL_MACHINE//SYSTEM//CurrentControlSet//Service//PerfProc//Performance下的:Disable Performance Counters 的值改为0
得到系统当前进程,加入到listBox中:
System.Diagnostics.Process[] processOnComputer = System.Diagnostics.Process.GetProcesses();
foreach ( System.Diagnostics.Process p in processOnComputer )
{
this.listBox1.Items.Add(p.ProcessName);
}
关闭某个指定的进程:
System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName(this.listBox1.SelectedItem.ToString());
foreach ( System.Diagnostics.Process p in process)
{
p.Kill();
}
但是这样会关闭进程的所有实例,比如如果你打开了多个IE,会把所有的IE窗口都关闭。
下面实现关闭某个特定的IE实例
先声明一个ArrayList:
ArrayList windowHandle = new ArrayList();
得到指定进程的所有实例,放到一个ListBox中,同时把主窗口的Handle放到ArrayList中:
System.Diagnostics.Process[] processOnComputer = System.Diagnostics.Process.GetProcessesByName(this.listBox1.SelectedItem.ToString());
foreach ( System.Diagnostics.Process p in processOnComputer )
{
this.listBox2.Items.Add(p.MainWindowTitle);//在ListBox中显示主窗体的标题
windowHandle.Add(p.MainWindowHandle);
}
把指定的进程的主窗口的Handle和ArrayList中的比对,如果符合就关闭
System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName(this.listBox1.SelectedItem.ToString());
foreach ( System.Diagnostics.Process p in process )
{
if ( p.MainWindowHandle == (System.IntPtr)(windowHandle[this.listBox2.SelectedIndex]))
{
p.Kill();
this.listBox2.Items.RemoveAt(this.listBox2.SelectedIndex);
}
}
这样可以关闭有主窗体的进程,但是没有主窗体的还不行
新闻热点
疑难解答