如何从Windows窗体执行控制台应用程序?
我想从 Windows 窗体加载事件运行控制台应用程序(例如 app.exe)。 我尝试过 System.Diagnostics.Process.Start(),但是在打开 app.exe 后,它会立即关闭它。
有什么方法可以运行 app.exe 并将其保持打开状态吗?
I want to run a console application (eg app.exe) from a windows form load event.
I'v tried System.Diagnostics.Process.Start(), But after it opens app.exe, it closes it immidiately.
Is there any way that I can run app.exe and leave it open?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
如果您只是希望控制台窗口保持打开状态,您可以使用如下命令运行它:
If you are just wanting the console window to stay open, you could run it with something like this command:
尝试这样做:
我认为这会让你得到你想要的行为。假设您不只是想在命令窗口中查看输出。如果您只想查看输出,那么您已经有该答案的多个版本。这就是您运行应用程序并保持控制台打开的方式。
希望这有帮助。祝你好运。
Try doing this:
I think this will get you the behavior you are trying for. Assuming you weren't just trying to see the output in the command window. If you just want to see the output, you have several versions of that answer already. This is just how you can run your app and keep the console open.
Hope this helps. Good luck.
如果 app.exe 不执行任何操作,或者快速完成其工作(即简单地打印“Hello World”并返回),它将按照您刚才解释的方式运行。如果您希望 app.exe 在完成工作后保持打开状态,请在控制台应用程序中放置某种完成消息,后跟
Console.ReadKey();
。If app.exe does nothing, or finishes its work quickly (i.e. simply prints "Hello World" and returns), it will behave the way you just explained. If you want app.exe to stay open after its work is done, put some sort of completion message followed by
Console.ReadKey();
in the console application.如果您可以更改 app.exe 的代码,只需添加
Console.In.Read()
即可使其等待按键。If you can change the code of app.exe, just add
Console.In.Read()
to make it wait for a key press.app.exe 可以以
Console.ReadLine()
结尾,假设它也是一个由您控制源代码的 C# 应用程序。app.exe can end with
Console.ReadLine()
assuming it too is a C# application where you control the source code.鉴于您的主/从应用程序设置,您遇到两个问题之一:
对于第一个问题,您需要等待/阻止进程完成(即
Process.WaitForExit()
)。对于第二个问题,听起来从属应用程序已经完成了它需要做的事情(或者抛出异常)并立即关闭,尝试从命令提示符中使用相同的参数运行它并检查输出。
You have one of two problems, given your master/slave application setup:
For the first problem, you need to wait/block for the process to complete (i.e.
Process.WaitForExit()
.For the second problem, it sounds like the slave app has done what it needs to (or thrown an exception) and is closing immediately. Try running it with the same parameters from a command prompt and check the output.
如果您可以控制 app.exe,您应该了解它的功能,因此我假设您无法控制它的内部工作原理。在这种情况下,您可以尝试传递一个帮助标志,该标志可能会也可能不会为您提供有关如何调用 app.exe 的更多信息。尝试这样的事情:
If you have control over app.exe, you should be aware of how it functions so I will assume that you do not have control over it's inner workings. In that case, you can try passing a help flag which may or may not give you more info on how to call app.exe. Try something like this:
创建一个新的文本文件,将其命名为 app.bat 并将其放入其中:
现在让您的表单指向该 bat 文件。
Create a new text file, name it app.bat and put this in there:
Now have your form point to that bat file.
在控制台应用程序中,键入:
< code>Console.ReadLine(); - 使用这段代码等待,直到按 Enter
Console.ReadKey();
- 使用此代码等待按下某个键In your console application, type:
Console.ReadLine();
- Use this piece of code to wait until you press enterConsole.ReadKey();
- Use this code to wait until you press a key