如何从Windows窗体执行控制台应用程序?

发布于 2024-08-28 17:10:20 字数 150 浏览 3 评论 0原文

我想从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

梦境 2024-09-04 17:10:20

如果您只是希望控制台窗口保持打开状态,您可以使用如下命令运行它:

System.Diagnostics.Process.Start( @"cmd.exe", @"/k c:\path\my.exe" );

If you are just wanting the console window to stay open, you could run it with something like this command:

System.Diagnostics.Process.Start( @"cmd.exe", @"/k c:\path\my.exe" );
浮世清欢 2024-09-04 17:10:20

尝试这样做:

        string cmdexePath = @"C:\Windows\System32\cmd.exe";
        //notice the quotes around the below string...
        string myApplication = "\"C:\\Windows\\System32\\ftp.exe\"";
        //the /K keeps the CMD window open - even if your windows app closes
        string cmdArguments = String.Format("/K {0}", myApplication);
        ProcessStartInfo psi = new ProcessStartInfo(cmdexePath, cmdArguments);
        Process p = new Process();
        p.StartInfo = psi;
        p.Start();

我认为这会让你得到你想要的行为。假设您不只是想在命令窗口中查看输出。如果您只想查看输出,那么您已经有该答案的多个版本。这就是您运行应用程序并保持控制台打开的方式。

希望这有帮助。祝你好运。

Try doing this:

        string cmdexePath = @"C:\Windows\System32\cmd.exe";
        //notice the quotes around the below string...
        string myApplication = "\"C:\\Windows\\System32\\ftp.exe\"";
        //the /K keeps the CMD window open - even if your windows app closes
        string cmdArguments = String.Format("/K {0}", myApplication);
        ProcessStartInfo psi = new ProcessStartInfo(cmdexePath, cmdArguments);
        Process p = new Process();
        p.StartInfo = psi;
        p.Start();

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.

迷离° 2024-09-04 17:10:20

如果 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.

烟雨凡馨 2024-09-04 17:10:20

如果您可以更改 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.

薄荷梦 2024-09-04 17:10:20

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.

宣告ˉ结束 2024-09-04 17:10:20

鉴于您的主/从应用程序设置,您遇到两个问题之一:

  1. 您的主应用程序正在打开,显示一个表单,该表单运行从应用程序并立即关闭,即使从应用程序仍在运行。
  2. 您的主应用程序正在打开,显示一个表单,该表单运行从属应用程序,该应用程序立即关闭。

对于第一个问题,您需要等待/阻止进程完成(即Process.WaitForExit())。

对于第二个问题,听起来从属应用程序已经完成了它需要做的事情(或者抛出异常)并立即关闭,尝试从命令提示符中使用相同的参数运行它并检查输出。

You have one of two problems, given your master/slave application setup:

  1. Your master app is opening, displaying a form, that form runs the slave app and closes immediately, even though the slave app is still running.
  2. Your master app is opening, displaying a form, that form runs the slave app which closes immediately.

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.

随心而道 2024-09-04 17:10:20

如果您可以控制 app.exe,您应该了解它的功能,因此我假设您无法控制它的内部工作原理。在这种情况下,您可以尝试传递一个帮助标志,该标志可能会也可能不会为您提供有关如何调用 app.exe 的更多信息。尝试这样的事情:

private startApp()
{
    string command = " -h"; //common help flag for console apps
    System.Diagnostics.Process pRun;
    pRun = new System.Diagnostics.Process();
    pRun.EnableRaisingEvents = true;
    pRun.Exited += new EventHandler(pRun_Exited);
    pRun.StartInfo.FileName = "app.exe";
    pRun.StartInfo.Arguments = command;
    pRun.StartInfo.WindowStyle =  System.Diagnostics.ProcessWindowStyle.Normal

    pRun.Start();
    pRun.WaitForExit();
}
private void  pRun_Exited(object sender, EventArgs e)
{
    //Do Something Here
}

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:

private startApp()
{
    string command = " -h"; //common help flag for console apps
    System.Diagnostics.Process pRun;
    pRun = new System.Diagnostics.Process();
    pRun.EnableRaisingEvents = true;
    pRun.Exited += new EventHandler(pRun_Exited);
    pRun.StartInfo.FileName = "app.exe";
    pRun.StartInfo.Arguments = command;
    pRun.StartInfo.WindowStyle =  System.Diagnostics.ProcessWindowStyle.Normal

    pRun.Start();
    pRun.WaitForExit();
}
private void  pRun_Exited(object sender, EventArgs e)
{
    //Do Something Here
}
可爱咩 2024-09-04 17:10:20

创建一个新的文本文件,将其命名为 app.bat 并将其放入其中:

app.exe
pause

现在让您的表单指向该 bat 文件。

Create a new text file, name it app.bat and put this in there:

app.exe
pause

Now have your form point to that bat file.

青衫负雪 2024-09-04 17:10:20

在控制台应用程序中,键入:

< code>Console.ReadLine(); - 使用这段代码等待,直到按 Enter

Console.ReadKey(); - 使用此代码等待按下某个键

In your console application, type:

Console.ReadLine(); - Use this piece of code to wait until you press enter

Console.ReadKey(); - Use this code to wait until you press a key

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文