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

 

posted @ 2021-02-05 11:26  孤独の巡礼  阅读(514)  评论(0编辑  收藏  举报