Loading

C#小结之进程

进程寻找与终止

        //寻找进程
        public bool ExProgress(string name)
        {
            bool init = false;
            try
            {
                Process[] prc = Process.GetProcesses();
                foreach (Process pr in prc) //遍历整个进程
                {
                    if (name == pr.ProcessName) //如果进程存在
                    {
                        //LogHelp.WriteInLog("接口已经存在:" + pr.ProcessName.ToString(), true);
                        return true;
                    }
                    else
                    {
                        init = false;
                    }
                }
                return init;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool KillProgress(string name)
        {
            bool init = false;
            try
            {
                Process[] prc = Process.GetProcesses();
                foreach (Process pr in prc) //遍历整个进程
                {
                    if (name == pr.ProcessName) //如果进程存在
                    {
                        pr.Kill();
                        return true;
                    }
                    else
                    {
                        init = false;
                    }
                }
                return init;
            }
            catch (Exception)
            {
                return false;
            }
        }

 


posted @ 2012-05-16 15:40  魍魉_198x  阅读(148)  评论(0编辑  收藏  举报