c#如何啟動/幹掉/尋找 進程

來源:互聯網
上載者:User

尋找/列出進程很容易,但幹掉進程得藉助系統命令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://yjmyzz.cnblogs.com/");
        }

        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的下載

http://files.cnblogs.com/yjmyzz/ntsd.rar

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.