如何围绕控制台应用程序创建 C# 包装器(任何语言)
我正在尝试制作一个控制台应用程序,它可以包含几乎任何打开控制台的应用程序。
一些简单的事实:
- 包装应用程序的输出应显示在控制台中。
- 包装器控制台的输入也应该是包装控制台应用程序的输入。
- 您必须能够使用代码来插入命令。
这是我到目前为止所拥有的,除了最后几行之外,一切正常:
ProcessStartInfo startInfo = new ProcessStartInfo("someBatchThingy.cmd");
startInfo.WorkingDirectory = Environment.CurrentDirectory;
startInfo.UseShellExecute = false;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
Thread.Sleep(5000);
Stream s = Console.OpenStandardOutput();
byte[] stopCommand = new byte[] { 115, 116, 111, 112, 13 };
s.Write(stopCommand, 0, stopCommand.Length);
process.WaitForExit();
因为性能很重要,所以我真的想重新分配到进程输出的位置,而不是手动将数据从隐藏控制台传输到包装器控制台。
有谁知道如何做到这一点/如果可能的话?
I'm trying to make a console application, that can wrap around pretty much any application that opens a console.
A few quick facts:
- The output of the wrapped application should show in the console.
- The input of the wrapper console should also be the input of the wrapped console app.
- You must be able to use code to also insert commands.
This is what I have so far, everything works, except the last few lines:
ProcessStartInfo startInfo = new ProcessStartInfo("someBatchThingy.cmd");
startInfo.WorkingDirectory = Environment.CurrentDirectory;
startInfo.UseShellExecute = false;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
Thread.Sleep(5000);
Stream s = Console.OpenStandardOutput();
byte[] stopCommand = new byte[] { 115, 116, 111, 112, 13 };
s.Write(stopCommand, 0, stopCommand.Length);
process.WaitForExit();
Because performance matters a lot, I really want to reassign to where the process outputs, and not manually transfer the data from a hidden console, to the wrapper console.
Does anyone know how to do this/if this is possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
据我所知,如果不通过应用程序传输数据,就无法实现这一目标。
执行此操作的代码如下:
我的子应用程序的代码如下所示:
与往常一样,这会产生一些开销,尽管它在您正在包装的任何重要应用程序中可能微不足道。
如果您要发送大量数据,并且希望避免超出必要范围的刷新,则可以将缓冲区大小从 4KB 增加到适合您的大小。
So as far as I know there is no way to achieve this without tunneling the data through your application.
Code to do this is as follows:
My child app's code looks like:
As always this has some overhead, though it's likely insignificant in any non-trivial application you're wrapping around.
If you're sending a lot of data, and want to avoid flushing more than necessary, you can increase the buffer size from 4KB to whatever suits you.
老问题,但刚刚提出以下来解决类似的问题。
使用此类,您可以控制交互,而无需像这样的控制台应用程序。
Old question, but just up came up with following to solve a similar problem.
using this class you can control interaction without console app like so.
它的新版本会很漂亮..但至少这不依赖于发送密钥。
Its newer going to be pretty.. But atleast this does not rely on sendkeys.