C#Windows Form .Net和DOS控制台

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

谢谢

您可以从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( ); } 

该文档指出 ,如果要同时读取StandardError和StandardOutput,则需要异步读取其中一个以避免死锁。

此外,如果您在其中一个重定向流上调用ReadToEnd,则必须调用WaitForExit() 之前执行此操作。 如果在ReadToEnd之前有WaitForExit,则输出缓冲区可以填满,暂停进程,这意味着它永远不会退出。 那将是一个漫长的等待。 这也是在doc中!

例:

 string output; string error; System.Diagnostics.Process p = new System.Diagnostics.Process { StartInfo = { FileName = program, Arguments = args, WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden, UseShellExecute = false, } }; if (waitForExit) { StringBuilder sb = new StringBuilder(); p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; Action stdErrorRead = (o,e) => { if (!String.IsNullOrEmpty(e.Data)) sb.Append(e.Data); }; p.ErrorDataReceived += stdErrorRead; p.Start(); // begin reading stderr asynchronously p.BeginErrorReadLine(); // read stdout synchronously output = p.StandardOutput.ReadToEnd(); p.WaitForExit(); // return code is in p.ExitCode if (sb.Length > 0) error= sb.ToString(); } 

您应该首先添加对System.Diagnostics的引用,然后调用批处理文件,如下所示:

 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); 

现在,如果您希望阻止调用(IE,您的应用程序将冻结,直到文件完成),那么只需使用string result = proc.StandardOutput.ReadToEnd()来读取整个批处理文件的输出。

但是,如果您希望应用程序继续响应,以及实时显示输出,那么您将需要使用BeginOutputReadLine 。

我一直在使用System.Diagnostics.Process类来调用基于控制台的应用程序以及格式化和返回输出。 我认为它也适用于批处理文件。 我会花点时间来测试一下。 以下是一些示例代码:

  System.Diagnostics.ProcessStartInfo start = new System.Diagnostics.ProcessStartInfo(); start.UseShellExecute = false; start.RedirectStandardInput = true; start.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; start.RedirectStandardOutput = true; start.FileName = "at"; System.Diagnostics.Process myP = System.Diagnostics.Process.Start(start); String strOutput = myP.StandardOutput.ReadToEnd(); if (strOutput.Contains("There are no entries in the list.")) { litMsg.Text = "There are no jobs"; } else { strOutput = strOutput.Replace("\r", ""); foreach (String line in strOutput.Split("\n".ToCharArray())) { //(0,7) (7,5)(12, 24) (36, 14) (50, ) //Status ID Day Time Command Line //------------------------------------------------------------------------------- // 1 Tomorrow 3:00 AM dir * if (line.Length > 50) { String Status = line.Substring(0, 7); String ID = line.Substring(7, 5); String Day = line.Substring(12, 24); String Time = line.Substring(35, 14); String Command = line.Substring(49); } } } 

我最近构建了一个小应用程序,我在其中与批处理文件进行交互。 我发现这个代码的snippit允许我这样做:

 Process proc = new Process { StartInfo = { RedirectStandardError = true, RedirectStandardOutput = true, UseShellExecute = false, } }; proc.Start(); string errorMessage = proc.StandardError.ReadToEnd(); proc.WaitForExit(); string outputMessage = proc.StandardOutput.ReadToEnd(); proc.WaitForExit(); 

从这里开始,只需将这些字符串指向您选择的用户控件即可。


EDITS

注意 :这不是一般解决方案。 它可能导致死锁。 从RedirectStandardError的doc:
同步读取操作在从StandardError流读取调用者和写入该流的子进程之间引入依赖关系。 这些依赖项可能会导致死锁条件。 当调用者从子进程的重定向流中读取时,它依赖于子进程。 调用者等待读操作,直到子进程写入流或关闭流。 当子进程写入足够的数据来填充其重定向流时,它依赖于父进程。 子进程等待下一个写操作,直到父进程从完整流中读取或关闭流。 当调用者和子进程等待彼此完成操作时,将导致死锁条件,并且两者都不能继续。 您可以通过评估调用者和子进程之间的依赖关系来避免死锁。

我很确定有更好的选项,但您可以将命令的输出路由到临时文件

将命令行输出重定向到文件

对于显示大量文本的程序,请考虑将通常在stdout上显示的文本重定向到文件。 显示大量文本会降低执行速度; 在工作站上的终端窗口中滚动文本可能会导致I / O瓶颈(增加已用时间)并占用更多CPU时间。

以下命令显示如何通过将输出重定向到文件然后显示程序输出来更有效地运行程序:

myprog> results.lis more results.lis重定向程序的输出将改变报告的时间,因为屏幕I / O减少。

我认为你也可以将它路由到一个变量,但不是100%肯定。 可能有更好的选择,但至少这是一个。