test
test

Reputation: 421

How to keep Console Window open after execution?

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!

Upvotes: 3

Views: 12108

Answers (3)

unknown6656
unknown6656

Reputation: 2963

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);

Upvotes: 4

Ivan Stoev
Ivan Stoev

Reputation: 205529

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.

Upvotes: 1

Slashy
Slashy

Reputation: 1881

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.

Upvotes: 1

Related Questions