c#如何杀进程和启动进程

代码如下:

        static void StartProcess(string folder, string processName)
        {
            string path = Path.Combine(folder, processName);
            try
            {
                Process.Start(path);
            }
            catch (Exception ex)
            {
                Console.WriteLine(string.Format("Error:{0}\n{1}", ex.Message, ex.StackTrace));
            }
        }


        static void KillProcess(string processName)
        {
            if (string.IsNullOrEmpty(processName))
                return;
            processName = Path.GetFileNameWithoutExtension(processName).ToLower();
            foreach (Process p in Process.GetProcesses())
            {
                if (p.ProcessName.ToLower() != processName)
                    continue;

                try
                {
                    p.Kill();
                    p.WaitForExit();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(string.Format("Error:{0}\n{1}", ex.Message, ex.StackTrace));
                }
            }
        }

 

转载请注明出处:https://www.cnblogs.com/jietian331/p/14376769.html

 

上一篇:20210528# Java中try()...catch()用法


下一篇:如何在ASP.NET Core项目启动时执行异步定时任务