栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c#如何启动/干掉/查找 进程

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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);
            }            
        }

        /// 


        /// 列出所有可访问进程
        /// 

        /// 
        /// 
        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);
                }
               
            }
        }


        ///    
        /// 运行DOS命令   
        /// DOS关闭进程命令(ntsd -c q -p PID )PID为进程的ID   
        /// 
   
        ///    
        ///    
        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下复制过来继续使用

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/231625.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号