Keep console window of a new Process open after it finishes

前端 未结 4 1803
一向
一向 2020-12-01 10:15

I currently have a portion of code that creates a new Process and executes it from the shell.

Process p = new Process();
...
p.Start();
p.WaitForExit();
         


        
4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-01 11:11

    This will open the shell, start your executable and keep the shell window open when the process ends

    Process p = new Process();
    ProcessStartInfo psi = new ProcessStartInfo();
    psi.FileName = "CMD.EXE";
    psi.Arguments = "/K yourmainprocess.exe";
    p.StartInfo = psi;
    p.Start();
    p.WaitForExit();
    

    or simply

    ProcessStartInfo psi = new ProcessStartInfo();
    psi.FileName = "CMD.EXE";
    psi.Arguments = "/K yourmainprocess.exe";
    Process p = Process.Start(psi);
    if(p != null && !p.HasExited)
        p.WaitForExit();
    

提交回复
热议问题