C# 打开和关闭某exe进程

启动方法一:

    /// <summary>
    /// 启动其他程序
    /// </summary>
    /// <param name="FileName">需要启动的外部程序名称</param>
    /// <param name="Arguments">一个文件夹路径</param>
    public static bool OpenPress(string FileName, string Arguments)
    {
        print(FileName);
        Process pro = new Process();
        if (System.IO.File.Exists(FileName))
        {
            pro.StartInfo.FileName = FileName;
            pro.StartInfo.Arguments = Arguments;
            pro.Start();
            return true;
        }
        return false;
    }

启动方法二:

    //启动某exe
    public void StartPress()
    {
        ProcessStartInfo info = new ProcessStartInfo();
        info.FileName = @"F:/_FaBu/WS/MyBuild/22复旦/复旦科技园显示端002/02FDKJYQ_Show.exe";
        info.Arguments = "";
        //指定程序运行状态,最大化、最小化等
        info.WindowStyle = ProcessWindowStyle.Maximized;
        Process pro = Process.Start(info);
        pro.WaitForExit();
    }

关闭

    //关闭程序
    public void ClosePress(string name)
    {        
        //获得任务管理器中的所有进程
        Process[] process = Process.GetProcesses();
        foreach (Process p1 in process)
        {
            try
            {
                string processName = p1.ProcessName.ToLower().Trim();
                //判断是否包含阻碍更新的进程
                if (processName == name)
                {
                    p1.Kill();
                }
            }
            catch { }
        }
    }

路径中不能有空格

posted @ 2022-06-22 18:04  哒哒哒~~~  阅读(672)  评论(0编辑  收藏  举报