C++ - Hold the Console Window Open

How to keep the console window open in Visual C++?

Start the project with Ctrl+F5 instead of just F5.

The console window will now stay open with the Press any key to continue . . . message after the program exits.

Note that this requires the Console (/SUBSYSTEM:CONSOLE) linker option, which you can enable as follows:

  1. Open up your project, and go to the Solution Explorer. If you're following along with me in K&R, your "Solution" will be 'hello' with 1 project under it, also 'hello' in bold.
  2. Right click on the 'hello" (or whatever your project name is.)
  3. Choose "Properties" from the context menu.
  4. Choose Configuration Properties>Linker>System.
  5. For the "Subsystem" property in the right-hand pane, click the drop-down box in the right hand column.
  6. Choose "Console (/SUBSYSTEM:CONSOLE)"
  7. Click Apply, wait for it to finish doing whatever it does, then click OK. (If "Apply" is grayed out, choose some other subsystem option, click Apply, then go back and apply the console option. My experience is that OK by itself won't work.)

CTRL-F5 and the subsystem hints work together; they are not separate options.

(Courtesy of DJMorreTX from http://social.msdn.microsoft.com/Forums/en-US/vcprerelease/thread/21073093-516c-49d2-81c7-d960f6dc2ac6)

How to keep console window open

You forgot calling your method:

static void Main(string[] args)
{
StringAddString s = new StringAddString();
s.AddString();
}

it should stop your console, but the result might not be what you expected, you should change your code a little bit:

Console.WriteLine(string.Join(",", strings2));

Keeping Console Window Open in Visual Studio (C)

See also How to keep the console window open in Visual C++?

C++ - Hold the console window open?

How about std::cin.get(); ?

Also, if you're using Visual Studio, you can run without debugging (CTRL-F5 by default) and it won't close the console at the end. If you run it with debugging, you could always put a breakpoint at the closing brace of main().

How to keep Console Window open after execution?

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

Keep console window of a new Process open after it finishes

This will open the shell, start your executable and keep the shell window open when the process ends

Process p = new Process();
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "CMD.EXE";
psi.Arguments = "/K yourmainprocess.exe";
p.StartInfo = psi;
p.Start();
p.WaitForExit();

or simply

ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "CMD.EXE";
psi.Arguments = "/K yourmainprocess.exe";
Process p = Process.Start(psi);
if(p != null && !p.HasExited)
p.WaitForExit();

How to hold the console open in C?

puts("Press <enter> to quit:");
getchar();

That's assuming you need to do this in the program, which is probably not a good idea in general. And if I run your program from a shell, I'm going to be a bit annoyed at the extra step when I'm expecting the program to terminate nicely and let me have my next prompt.

Hold the console window open using c#

Just start the cmd process with MSBuild.exe as an argument instead of directly starting the exe file.

string process = @"C:\WINNT\Microsoft.NET\Framework\v2.0.50727\MSBuild.exe " + sourcePath + @" \Application.sln /t:rebuild";
System.Diagnostics.Process csc = System.Diagnostics.Process.Start(@"%windir%\system32\cmd.exe", process);

Microsoft Visual Studio: How to keep the console open without manually reading input?

Ctrl + F5
for quick test.
The key combination keeps the console open until you close it.



Related Topics



Leave a reply



Submit