C#操作系统进程的代码演示

2021-11-02 16:08:20 浏览数 (1)

这段代码演示了如何根据进程名关闭进程和启动进程

代码语言:javascript复制
private bool CloseProcess(string CloseProcessName)
        {
            try
            {
                //根据进程名称,获取该进程信息
                Process[] MyProcessS = Process.GetProcessesByName(CloseProcessName);

            foreach (Process MyProcess in MyProcessS)
            {
                MyProcess.Kill();
                MyProcess.WaitForExit();
                MyProcess.Close();
                Thread.Sleep(10000); 
            }
        }
        catch (Exception)
        {
            return false;                                
        }
        return true;                         
    }
    /// <summary>
    /// 创建进程
    /// </summary>
    public bool StartProcess(string StartProPath)
    {
        try
        {
            Process TheStartProcess = Process.Start(StartProPath);           
        }
        catch (Exception)
        {
            return false;                                   
        }

        return true;                                       
    }</pre> 

0 人点赞