how to execute console application from windows form?

后端 未结 8 905
感情败类
感情败类 2020-12-09 11:43

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 immi

相关标签:
8条回答
  • 2020-12-09 11:46

    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.

    0 讨论(0)
  • 2020-12-09 11:46

    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.

    0 讨论(0)
  • 2020-12-09 11:49

    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" );
    
    0 讨论(0)
  • 2020-12-09 11:51

    If you can change the code of app.exe, just add Console.In.Read() to make it wait for a key press.

    0 讨论(0)
  • 2020-12-09 11:52

    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.

    0 讨论(0)
  • 2020-12-09 12:02

    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.

    0 讨论(0)
提交回复
热议问题