简体   繁体   中英

how to execute console application from windows form?

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?

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

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

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.

If app.exe does nothing, or finishes its work quickly (ie 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()以使其等待按键。

app.exe可以以Console.ReadLine()结束,假设它也是一个C#应用程序,您可以在其中控制源代码。

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 (ie 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.

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
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM