C# Windows Form .Net and DOS Console

前端 未结 6 793
走了就别回头了
走了就别回头了 2020-12-04 02:50

I have a windows form that executes a batch file. I want to transfer everything that happends in my console to a panel in my form. How can I do this? How can my DOS console

6条回答
  •  无人及你
    2020-12-04 03:15

    You should start off by adding a reference to System.Diagnostics, then calling the batch file like this:

    string myFile = "c:\\path\\to\\batch_file.bat";
    ProcessStartInfo psi = new ProcessStartInfo(myFile);
    psi.UseShellExecute = false;
    psi.CreateNoWindow = true;
    psi.RedirectStandardOutput = true;
    Process proc = Process.Start(psi);
    

    Now, if you want the call to be blocking (I.E., your application will freeze until the file is done), then just use string result = proc.StandardOutput.ReadToEnd() to read the entirety of your batch file's output.

    However, if you want to have the application continue to respond, as well as display output in real-time, then you'll need to have to use BeginOutputReadLine.

提交回复
热议问题