Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 314
Or Betzalel Avatar asked Apr 07 '10 14:04

Or Betzalel


3 Answers

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" );
like image 162
Mark Wilkins Avatar answered Oct 21 '22 08:10

Mark Wilkins


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.

like image 42
Audie Avatar answered Oct 21 '22 08:10

Audie


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.

like image 2
Mark Carpenter Avatar answered Oct 21 '22 10:10

Mark Carpenter