C#Windows Form .Net和DOS控制台

use*_*958 4 .net c#

我有一个执行批处理文件的Windows窗体.我想将我控制台中发生的所有事情转移到我表单中的面板.我怎样才能做到这一点?我的DOS控制台如何与我的Windows窗体面板通信???

谢谢

Dou*_*rch 7

您可以从Form应用程序中调用DOS或批处理程序,并将输出重定向到字符串:

using (var p = new System.Diagnostics.Process( ))
{
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.FileName = PathToBatchFile;
    p.StartInfo.Arguments = args;
    p.Start( );
    string o = p.StandardOutput.ReadToEnd( );
    p.WaitForExit( );
}
Run Code Online (Sandbox Code Playgroud)

  • 你不能在ReadToEnd之前放置WaitForExit().进程的输出缓冲区可能会填满,这将暂停进程.这意味着它永远不会退出.因此,在WaitForExit()之前调用ReadToEnd,因为代码在上面. (4认同)