C# 实现打开、关闭进程

    技术2026-04-22  6

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Diagnostics;

     

    namespace ProcessOperate

    {

        class Program

        {

            static void Main(string[] args)

            {

                while (true)

                {

                    string strInputValue = Console.ReadLine();

                    //运行一个进程

                    if (strInputValue.ToLower() == "start")

                    {

                        Process process = new Process();

                        process.StartInfo.UseShellExecute = false;

                        process.StartInfo.CreateNoWindow = true;

                        process.StartInfo.FileName = @"F:/实例资源/自动更新 代码/AutoUpdaterDemo/bin/Debug/AutoUpdaterDemo.exe";

                        process.Start();

                    }

                    //关闭一个进程

                    if (strInputValue.ToLower() == "kill")

                    {

                        System.Diagnostics.Process[] myProcess = System.Diagnostics.Process.GetProcesses();

     

                        foreach (System.Diagnostics.Process pr in myProcess)

                        {

                            if (pr.ProcessName == "AutoUpdaterDemo")

                            {

                                pr.Kill();

                            }

                        }

                    }

                    if (strInputValue.ToLower() == "exit") return;

                }

            }

        }

    }

    最新回复(0)