C#實現(xiàn)啟動,關(guān)閉與查找進程的方法
本文實例講述了C#實現(xiàn)啟動,關(guān)閉與查找進程的方法。分享給大家供大家參考,具體如下:
運行效果截圖如下:

查找/列出進程很容易,但干掉進程得借助系統(tǒng)命令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://chabaoo.cn/");
}
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());//某些系統(tǒng)進程禁止訪問,所以要加異常處理
}
}
}
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關(guān)閉進程命令(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)上,出于安全考慮已經(jīng)被MS給去掉了,但我們可以直接從xp下復制過來繼續(xù)使用,這里為方便大家給出ntsd.exe的下載。
希望本文所述對大家C#程序設(shè)計有所幫助。
相關(guān)文章
asp.net(c#)編程實現(xiàn)將彩色圖片變灰階圖片的方法示例
這篇文章主要介紹了asp.net(c#)編程實現(xiàn)將彩色圖片變灰階圖片的方法,結(jié)合實例形式分析了C#圖片讀取及屬性操作相關(guān)技巧,需要的朋友可以參考下2017-07-07
C#將Sql數(shù)據(jù)保存到Excel文件中的方法
這篇文章主要介紹了C#將Sql數(shù)據(jù)保存到Excel文件中的方法,文中的ExportExcel可起到將sql數(shù)據(jù)導出為Excel的作用,需要的朋友可以參考下2014-08-08
C#中序列化實現(xiàn)深拷貝,實現(xiàn)DataGridView初始化刷新的方法
下面小編就為大家?guī)硪黄狢#中序列化實現(xiàn)深拷貝,實現(xiàn)DataGridView初始化刷新的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02

