亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

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://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下复制过来继续使用

點擊查看更多內容
1人點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消