C#完成啟動,封閉與查找過程的辦法。本站提示廣大學習愛好者:(C#完成啟動,封閉與查找過程的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成啟動,封閉與查找過程的辦法正文
本文實例講述了C#完成啟動,封閉與查找過程的辦法。分享給年夜家供年夜家參考,詳細以下:
運轉後果截圖以下:
查找/列出過程很輕易,但干失落過程得借助體系敕令ntsd.exe,具體用法見上面的代碼 :
using System; using System.Diagnostics; using System.Windows.Forms; namespace ProcessDemo { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { this.linkLabel1.Links.Add(0, linkLabel1.Text.Length, "http://www.jb51.net/"); } private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { this.linkLabel1.Links[linkLabel1.Links.IndexOf(e.Link)].Visited = true; string target = e.Link.LinkData as string; if (target != null && target.StartsWith("http://")) { Process.Start(target); } } /// <summary> /// 列出一切可拜訪過程 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnList_Click(object sender, EventArgs e) { Process[] processes; processes = Process.GetProcesses(); string str = ""; foreach (Process p in processes) { try { str = p.ProcessName; this.lst1.Items.Add("稱號:" + p.ProcessName + ",啟動時光:" + p.StartTime.ToShortTimeString() + ",過程ID:" + p.Id.ToString() ); } catch (Exception ex) { this.lst1.Items.Add(ex.Message.ToString());//某些體系過程制止拜訪,所以要加異常處置 } } } private void btnFind_Click(object sender, EventArgs e) { txtFind.Text = txtFind.Text.Trim().ToLower(); if (txtFind.Text.Length > 0) { Process[] arrP = Process.GetProcesses(); foreach (Process p in arrP) { try { if (p.ProcessName.ToLower() == txtFind.Text) { MessageBox.Show(txtFind.Text + " 找到了,PID為 " + p.Id.ToString()); return; } } catch { } } MessageBox.Show("未找到該過程,請檢討輸出!"); } } private void btnKill_Click(object sender, EventArgs e) { txtFind.Text = txtFind.Text.Trim().ToLower(); int pid = -1; if (txtFind.Text.Length > 0) { Process[] arrP = Process.GetProcesses(); foreach (Process p in arrP) { try { if (p.ProcessName.ToLower() == txtFind.Text) { pid = p.Id; break; } } catch { } } if (pid != -1) { RunCmd("ntsd -c q -p " + pid); } } } /// <summary> /// 運轉DOS敕令 /// DOS封閉過程敕令(ntsd -c q -p PID )PID為過程的ID /// </summary> /// <param name="command"></param> /// <returns></returns> public string RunCmd(string command) { Process p = new Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.Arguments = "/c " + command; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.CreateNoWindow = true; p.Start(); return p.StandardOutput.ReadToEnd(); } } }
別的ntsd.exe在windows vista以上的版本(包含windows 2008)上,出於平安斟酌曾經被MS給去失落了,但我們可以直接從xp下復制過去持續應用,這裡為便利年夜家給出ntsd.exe的下載。
願望本文所述對年夜家C#法式設計有所贊助。