Run console application from other console app

前端 未结 6 1927
耶瑟儿~
耶瑟儿~ 2020-12-02 18:40

I have a C# console application (A). I want to execute other console app (B) from within app A (in synchronous manner) in such way that B uses the same command window. When

相关标签:
6条回答
  • 2020-12-02 19:05

    You can start another process with Process.Start - doesn't really matter if it's a console app or not. If your app is already running in a console window the newly spawned app will use that console window as well.

    var proc = Process.Start( "...path to second app" );
    proc.WaitForExit();
    var exitCode = proc.ExitCode;
    

    Be sure to ready the docs on the Process class as there are a variety of little nuances that may affect the way your secondary app is launched.

    0 讨论(0)
  • 2020-12-02 19:09

    You can use Process.Start to start the other console application.

    You will need to construct the process with ProcessStartInfo.RedirectOutput set to true and UseShellExecute set to false in order to be able to utilize the output yourself.

    You can then read the output using StandardOutput.ReadToEnd on the process.

    0 讨论(0)
  • 2020-12-02 19:09

    You can start another process using the Process.Start() call. The examples here show how to read output from other process and wait for it to finish.

    0 讨论(0)
  • 2020-12-02 19:17

    Fill out a System.Diagnostics.ProcessStartInfo and pass it to Process.Start

    You can WaitForExit on the resulting process, and use then use ExitCode property of the process to see the return value.

    0 讨论(0)
  • 2020-12-02 19:17

    you can "wrap" the old console app with the new one by including it in your references and starting it off by calling whatever method is called in the run method of the program class

    0 讨论(0)
  • 2020-12-02 19:20

    I was able to run the program 'B' as part of the same command window by calling the following configuration:

    ConsoleColor color = Console.ForegroundColor;
    ProcessStartInfo startinfo = new ProcessStartInfo(nameProgramB);
    startinfo.CreateNoWindow = false;
    startinfo.UseShellExecute = false;
    Process p = Process.Start(startinfo);
    p.WaitForExit();
    Console.ForegroundColor = color;
    

    this way, both programs run seamlesly like they were one single program. 'nameProgramB' is the name to program 'B'. Hope this helps.

    0 讨论(0)
提交回复
热议问题