C# Windows Form .Net and Dos Console

Embedding a DOS console in a windows form

It's possible to redirect the standard input/output of console/dos applications using the Process class. It might look something like this:

var processStartInfo = new ProcessStartInfo("someoldapp.exe", "-p someparameters");

processStartInfo.UseShellExecute = false;
processStartInfo.ErrorDialog = false;

processStartInfo.RedirectStandardError = true;
processStartInfo.RedirectStandardInput = true;
processStartInfo.RedirectStandardOutput = true;
processStartInfo.CreateNoWindow = true;

Process process = new Process();
process.StartInfo = processStartInfo;
bool processStarted = process.Start();

StreamWriter inputWriter = process.StandardInput;
StreamReader outputReader = process.StandardOutput;
StreamReader errorReader = process.StandardError;

You can now use the streams to interact with the application.
By setting processStartInfo.CreateNoWindow to true the original application will be hidden.

I hope this helps.

Embedding a DOS console in a windows form with Visual Basic

Using bring a console window to front in c# as a basis, you can modify your code:

<DllImport("user32.dll", EntryPoint:="FindWindow", SetLastError:=True)> _
Private Shared Function FindWindowByCaption(ByVal zeroOnly As IntPtr, ByVal lpWindowName As String) As IntPtr
End Function

''in frmLoad:
hwnd = FindWindowByCaption(IntPtr.Zero, "c:\WINDOWS\system32\cmd.exe")

As Jon Skeet said:

It's hacky, it's horrible, but it works for me (thanks, pinvoke.net!):

And Cody Gray is also correct with this:

You probably can't manage to find it because it won't always have this title: C:\\WINDOWS\\system32\\cmd.exe. Mine doesn't, for example.

So it works, but is flaky.

How do I show a console output/window in a forms application?

this one should work.

using System.Runtime.InteropServices;

private void Form1_Load(object sender, EventArgs e)

[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool AllocConsole();

How to use Console.WriteLine() in windows Forms Application

Console.WriteLine command is working only in console application type. so using windows app doesn't display required output,seems your application is windows form application.

if you want show output on Console window using Console.WriteLine withing the windows form application need to add this property and call it from the main form constructor.then it will open with console as well.

     public InsertNames()

private static extern bool AllocConsole();


In project settings you can set application type as Console. Then you will get console and form as well.

Running a C# console application in dos?

I recently got an old dos/windows 3.1 laptop. I'd like to know if it'd be possible to run my console applications made in c# in it.


Windows 3.1 shipped in 1992 and is a 16 bit operating system. C# 1.0 shipped in 2002, ten years later, and requires a 32 bit operating system.

If it's not, what is the best option for someone with the very basic knowlegde of c# to write some little programs for dos or windows 3.1?

Learn Basic instead. Several versions of Basic are available for Windows 3.1.

C# application both GUI and commandline

  1. Edit your project properties to make your app a "Windows Application" (not "Console Application"). You can still accept command line parameters this way. If you don't do this, then a console window will pop up when you double-click on the app's icon.
  2. Make sure your Main function accepts command line parameters.
  3. Don't show the window if you get any command line parameters.

Here's a short example:

static void Main(string[] args)
if(args.Length == 0)
Application.Run(new MyMainForm());
// Do command line/silent logic here...

If your app isn't already structured to cleanly do silent processing (if all your logic is jammed into your WinForm code), you can hack silent processing in ala CharithJ's answer.

Sorry to hijack your answer Merlyn. Just want all the info here for others.

To be able to write to console in a WinForms app just do the following:

static class Program
// defines for commandline output
static extern bool AttachConsole(int dwProcessId);
private const int ATTACH_PARENT_PROCESS = -1;

/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main(string[] args)
// redirect console output to parent process;
// must be before any calls to Console.WriteLine()

if (args.Length > 0)
Console.WriteLine("Yay! I have just created a commandline tool.");
// sending the enter key is not really needed, but otherwise the user thinks the app is still running by looking at the commandline. The enter key takes care of displaying the prompt again.
Application.Run(new QrCodeSampleApp());

How do I pass command-line arguments to a WinForms application?

static void Main(string[] args)
// For the sake of this example, we're just printing the arguments to the console.
for (int i = 0; i < args.Length; i++) {
Console.WriteLine("args[{0}] == {1}", i, args[i]);

The arguments will then be stored in the args string array:

$ AppB.exe firstArg secondArg thirdArg
args[0] == firstArg
args[1] == secondArg
args[2] == thirdArg

Related Topics

Leave a reply