3

It's a little bit complicated problem. I have tried probably everything and still no working. I run WinForm application from I run CMD and next run another app(console application) on cmd. It's working on /c START xyz but when app finished CMD always is closing. I want to pause this window.

ProcessStartInfo processInfo = new ProcessStartInfo { FileName = "cmd.exe", WorkingDirectory = Path.GetDirectoryName(YourApplicationPath), Arguments = "/K START " + cmdparametr, RedirectStandardOutput = true, RedirectStandardInput = true, RedirectStandardError = true, CreateNoWindow = false, UseShellExecute = false, WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal, }; Process p = new Process { StartInfo = processInfo }; p.Start(); int ExitCode; p.WaitForExit(); // *** Read the streams *** string output = p.StandardOutput.ReadToEnd(); string error = p.StandardError.ReadToEnd(); ExitCode = p.ExitCode; MessageBox.Show("output>>" + (String.IsNullOrEmpty(output) ? "(none)" : output)); MessageBox.Show("error>>" + (String.IsNullOrEmpty(error) ? "(none)" : error)); MessageBox.Show("ExitCode: " + ExitCode.ToString(), "ExecuteCommand"); p.Close(); 

ReadStream is working when I add argument: START /b but I think it's not important.
WaitForExit() doesn't work.

Is it possible to pause application through command maybe like this: /k start xyz.exe & PAUSE?


My app is console application!

1
  • Have you tried just appending & pause to the end of your command as your question notes? Perhaps related - is there any reason you are redirecting input? Commented Oct 5, 2015 at 18:00

3 Answers 3

4

You can use the pause-command inside C# if you want: I use it as follows:

Solution №1: (Uses P/Invoke)

// somewhere in your class [DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl, SetLastError=true)] public static extern int system(string command); public static int Main(string[] argv) { // your code system("pause"); // will automatically print the localized string and wait for any user key to be pressed return 0; } 

Solution №2:

Console.WriteLine("Press any key to exit..."); Console.ReadLine(true); 

**EDIT**: you can create a temporary batch file dynamically and execute it, for example:
string bat_path = "%temp%/temporary_file.bat"; string command = "command to be executed incl. arguments"; using (FileStream fs = new FileStream(bat_path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read)) using (StreamWriter sw = new StreamWriter(fs, Encoding.Default)) { sw.WriteLine("@echo off"); sw.WriteLine(command); sw.WriteLine("PAUSE"); } ProcessStartInfo psi = new ProcessStartInfo() { WorkingDirectory = Path.GetDirectoryName(YourApplicationPath), RedirectStandardOutput = true, RedirectStandardInput = true, RedirectStandardError = true, CreateNoWindow = false, UseShellExecute = false, WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal }; Process p = new Process() { StartInfo = psi; }; p.Start(); int ExitCode; p.WaitForExit(); // *** Read the streams *** string output = p.StandardOutput.ReadToEnd(); string error = p.StandardError.ReadToEnd(); ExitCode = p.ExitCode; MessageBox.Show("output>>" + (String.IsNullOrEmpty(output) ? "(none)" : output)); MessageBox.Show("error>>" + (String.IsNullOrEmpty(error) ? "(none)" : error)); MessageBox.Show("ExitCode: " + ExitCode.ToString(), "ExecuteCommand"); p.Close(); File.Delete(bat_path); 
Sign up to request clarification or add additional context in comments.

2 Comments

It's Windows forms application, not Console Application.
Thank you!! This is it what I wanted to achieve.
1

For preventing the close of a Console Application you could use :

Console.ReadLine(); 

It would wait for for any key and would not close immediately.

2 Comments

Cannot read keys when either application does not have a console or when console input has been redirected from a file. Try Console.Read.
doesn't work... If I use only /c START xyz I get one window and when it finished it close window.
1

Do not include START command, use something like this

processInfo.Arguments = "/K " + your_console_app_exe_path_and_args; 

Make sure to enclose with double quotes where needed.

2 Comments

Nope, without parameter START I haven't seen any result of console application.
That's because you are redirecting the output of your console app.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.