简体   繁体   中英

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!

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

Solution №1:

//optional: Console.WriteLine("Press any key ...");
Console.ReadLine(true);

Solution №2: (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 automaticly print the localized string and wait for any user key to be pressed
    return 0;
}


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

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.

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.

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