码迷,mamicode.com
首页 > Windows程序 > 详细

C#关闭进程的2种方法

时间:2016-12-12 07:38:39      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:str   stop   esb   foreach   new   main   ring   try   public   

       private void KillProcess(string processName)
        {
            System.Diagnostics.Process myproc = new System.Diagnostics.Process();
            //得到所有打开的进程   
            try
            {
                foreach (Process thisproc in Process.GetProcessesByName(processName))
                {
                    //找到程序进程,kill之。
                    if (!thisproc.CloseMainWindow())
                    {
                        thisproc.Kill();
                    }
                }

            }
            catch (Exception Exc)
            {
                MessageBox.Show(Exc.Message);
            }
        }


        public static void StopProcess(string processName)
        {
            try
            {
                System.Diagnostics.Process[] ps = System.Diagnostics.Process.GetProcessesByName(processName);
                foreach (System.Diagnostics.Process p in ps)
                {
                    p.Kill();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

C#关闭进程的2种方法

标签:str   stop   esb   foreach   new   main   ring   try   public   

原文地址:http://www.cnblogs.com/sqlfeng/p/6161156.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!