C#判断某程序是否正在运行

时间:2021-03-22 04:59:03
public void isRun()//判断某程序是否正在运行
        {
            //取得指定进程
            Process[] MyProcess = Process.GetProcessesByName("你要进行判断的程序名称");
            int i = MyProcess.Length;
            if (i != 0)//资源管理器没有运行
            {
                MyProcess[0].EnableRaisingEvents = true;
                //如果进程没有关闭
                if (!MyProcess[0].HasExited)
                {
                    //如果关闭进程主窗口失败
                    if (!MyProcess[0].CloseMainWindow())
                    {
                        try
                        {//关闭进程
                            MyProcess[0].Kill();
                        }
                        catch
                        {
                            MessageBox.Show("程序加载出错!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                }
            }
        }