首页 > 代码库 > C# 开启及停止进程

C# 开启及停止进程

1.本篇内容转发自http://www.cnblogs.com/gaoyuchuanIT/articles/2946314.html

2. 首先在程序中引用:    System.Diagnostics;

        3. 开启进程:
        /// <summary>
        /// 开启进程
        /// </summary>
        /// <param name="aProPath">进程路径(如:@"C:\WINDOWS\system32\osk.exe")</param>
        /// <returns>开启成功(true)或失败(false)</returns>
        public bool StartProcess(string aProPath)
        {
            try
            {
                Process ps = Process.Start(aProPath);//开启进程
            }
            catch (Exception)
            {
                return false;                                    //失败
            }
            return true;                                        //成功
        }

        4. 停止进程:
                 /// <summary>
        /// 关闭进程
        /// </summary>
        /// <param name="aName">进程名称(不带.exe)</param>
        /// <returns>关闭成功(true)或失败(false)</returns>
        public bool CloseProcess(string aName)
        {
            try
            {
                Process[] ps = Process.GetProcessesByName(aName);//根据进程名称,获取该进程信息
                foreach (Process p in ps)
                {
                    p.Kill();
                    p.WaitForExit();
                    p.Close();                         //关闭
                } 
            }
            catch (Exception ex)
            {
                return false;                         //失败
            }
            return true;                             //成功
        }

C# 开启及停止进程