有没有办法在C#中的Windows窗体应用程序中运行批处理文件?

时间:2021-09-13 02:16:59

So anyways, I've been working on a batch IDE, and I was wondering if there was a good way to effectively embed the file into the form.

所以无论如何,我一直在研究批处理IDE,我想知道是否有一种很好的方法可以有效地将文件嵌入到表单中。

It would function sort of like a debug mode, where at any time, the user can click a button, and the batch file would load into the actual form.

它的功能类似于调试模式,用户可以随时单击按钮,批处理文件将加载到实际表单中。

Like the black cmd window would be embedded into the form... Is there any way to do that?

就像黑色cmd窗口将嵌入到表单中......有什么办法可以做到这一点吗?

2 个解决方案

#1


0  

ProcessStartInfo psi = new ProcessStartInfo();
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
psi.FileName = "C:\\echo.cmd";
var p = Process.Start(psi);
Console.WriteLine(p.StandardOutput.ReadToEnd());

And in C:\echo.cmd I have just basic echo hello!. When this code is executed - you'll see hello! received from batch's output stream.

在C:\ echo.cmd我只有基本的回声你好!执行此代码时 - 你会看到你好!从批处理的输出流中收到。

Note that if executed command will wait for some input - ReadToEnd() can't return. In this case you should use Process.OutputDataReceived event.

注意,如果执行的命令将等待某些输入 - ReadToEnd()无法返回。在这种情况下,您应该使用Process.OutputDataReceived事件。

#2


0  

Look at the process object and the StandardInput, StandardOutput and StandardError streams. That is essentially all the command window is showing with some special handling of control characters.

查看流程对象以及StandardInput,StandardOutput和StandardError流。这基本上是所有命令窗口都显示一些特殊的控制字符处理。

#1


0  

ProcessStartInfo psi = new ProcessStartInfo();
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
psi.FileName = "C:\\echo.cmd";
var p = Process.Start(psi);
Console.WriteLine(p.StandardOutput.ReadToEnd());

And in C:\echo.cmd I have just basic echo hello!. When this code is executed - you'll see hello! received from batch's output stream.

在C:\ echo.cmd我只有基本的回声你好!执行此代码时 - 你会看到你好!从批处理的输出流中收到。

Note that if executed command will wait for some input - ReadToEnd() can't return. In this case you should use Process.OutputDataReceived event.

注意,如果执行的命令将等待某些输入 - ReadToEnd()无法返回。在这种情况下,您应该使用Process.OutputDataReceived事件。

#2


0  

Look at the process object and the StandardInput, StandardOutput and StandardError streams. That is essentially all the command window is showing with some special handling of control characters.

查看流程对象以及StandardInput,StandardOutput和StandardError流。这基本上是所有命令窗口都显示一些特殊的控制字符处理。