先修改注冊表:
HKEY_LOCAL_MACHINE//SYSTEM//CurrentControlSet//Service//PerfProc//Performance下的:Disable Performance Counters 的值改為0
得到系統(tǒng)當(dāng)前進(jìn)程,加入到listBox中:
System.Diagnostics.Process[] processOnComputer = System.Diagnostics.Process.GetProcesses();
foreach ( System.Diagnostics.Process p in processOnComputer )
{
this.listBox1.Items.Add(p.ProcessName);
}
關(guān)閉某個指定的進(jìn)程:
System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName(this.listBox1.SelectedItem.ToString());
foreach ( System.Diagnostics.Process p in process)
{
p.Kill();
}
但是這樣會關(guān)閉進(jìn)程的所有實例,比如如果你打開了多個IE,會把所有的IE窗口都關(guān)閉。
下面實現(xiàn)關(guān)閉某個特定的IE實例
先聲明一個ArrayList:
ArrayList windowHandle = new ArrayList();
得到指定進(jìn)程的所有實例,放到一個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中顯示主窗體的標(biāo)題
windowHandle.Add(p.MainWindowHandle);
}
把指定的進(jìn)程的主窗口的Handle和ArrayList中的比對,如果符合就關(guān)閉
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);
}
}
這樣可以關(guān)閉有主窗體的進(jìn)程,但是沒有主窗體的還不行
新聞熱點(diǎn)
疑難解答