【问题标题】:How do I end a process that was started with ProcessStartInfo?如何结束使用 ProcessStartInfo 启动的进程?
【发布时间】:2020-10-25 21:24:55
【问题描述】:

我想结束一个使用 ProcessStartInfo 启动的进程,这是启动它的代码

public class MyProcess
{
    void OpenWithStartInfo()
    {
        ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe");
        startInfo.WindowStyle = ProcessWindowStyle.Hidden;
        startInfo.UseShellExecute = true;
        startInfo.FileName = "C:/StartBot.bat";
        Process.Start(startInfo);
    }

    public static void Start()
    {
        MyProcess myProcess = new MyProcess();
        myProcess.OpenWithStartInfo();
    }
    public static void End()
    {
               
    }
}

我想结束我使用 End() 函数开始的过程,但我不知道该怎么做。任何帮助将不胜感激。

【问题讨论】:

标签: c# wpf xaml


【解决方案1】:

Process.Start 返回一个Process,假设您将引用存储在某处,您可以调用Kill()

    public class MyProcess
    {
        Process process;
        void OpenWithStartInfo()
        {
            ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe");
            startInfo.WindowStyle = ProcessWindowStyle.Hidden;
            startInfo.UseShellExecute = true;
            startInfo.FileName = "C:/StartBot.bat";
            process = Process.Start(startInfo);
        }

        static MyProcess myProcess = new MyProcess();

        public static void Start()
        {
            myProcess.OpenWithStartInfo();
        }
        public static void End()
        {
            myProcess.process.Kill();
        }
    }

【讨论】:

    猜你喜欢
    • 2014-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-16
    • 2013-02-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多