Run a console application without DOS box

The example shows how to run a console application without the appearance of a DOS box.
It also shows how to retrieve the console output.

// This snippet needs the "System.Diagnostics"
// library
 
// Application path and command line arguments
string ApplicationPath = "C:\\example.exe";
string ApplicationArguments = "-c -x";
 
// Create a new process object
Process ProcessObj = new Process();
 
// StartInfo contains the startup information of
// the new process
ProcessObj.StartInfo.FileName = ApplicationPath;
ProcessObj.StartInfo.Arguments = ApplicationArguments;
 
// These two optional flags ensure that no DOS window
// appears
ProcessObj.StartInfo.UseShellExecute = false;
ProcessObj.StartInfo.CreateNoWindow = true;
 
// If this option is set the DOS window appears again :-/
// ProcessObj.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
 
// This ensures that you get the output from the DOS application
ProcessObj.StartInfo.RedirectStandardOutput = true;
 
// Start the process
ProcessObj.Start();
 
// Wait that the process exits
ProcessObj.WaitForExit();
 
// Now read the output of the DOS application
string Result = ProcessObj.StandardOutput.ReadToEnd();
Snippet information

Author:
Jonas John

License:
Public domain

Language:
C#

Created:
08/22/2007

Updated:
08/22/2007

Tags:
, ,

Related links:
- How to start a console app...




Sorry folks, comments have been deactivated for now due to the large amount of spam.

Please try to post your questions or problems on a related programming board, a suitable mailing list, a programming chat-room,
or use a QA website like stackoverflow because I'm usually too busy to answer any mails related
to my code snippets. Therefore please just mail me if you found a serious bug... Thank you!


Older comments:

Kirubaharan Palani April 18, 2011 at 13:05
Very nice post... Very helpful for my project
vishal March 23, 2011 at 11:16
it's very help ful in my project
thank you
nawfal March 14, 2011 at 06:35
@abhinav (and all those who see an exception handling error)

the reason is in this given example, applicationpath is C:\example.exe . set this to a valid path, where u want to run the command. also set the applicationarguments to a valid argument used in commandprompt. like edit, or cls etc.
nawfal March 14, 2011 at 06:31
this was so helpful. thanks a ton
ashwani September 30, 2010 at 09:12
i want to execute some dos based command using c# but i m facing some problem in doing that .........
plzzzz rectify my problems....

Thanks...
visualbasic July 15, 2010 at 04:17
The prior submission had its backslashes stripped. The following two lines need backslashes (here I will use forward slashes to illustrate their positions):

'%SystemRoot%/Microsoft.NET/Framework/v2.0.50727/vbc.exe %buildParam%

Dim ApplicationPath As String = "C:/WINDOWS/system32/arp.exe"
visualbasic July 15, 2010 at 04:11
For those using VB.NET who would like an example of wrapping a console application to collect its output while suppressing the console window, here is a quick translation:

'#FILE BUILD.CMD
'SET buildParam=/nologo /target:winexe "%~1"
'%SystemRoot%Microsoft.NETFrameworkv2.0.50727vbc.exe %buildParam%
'#ENDFILE

' This snippet needs the "System.Diagnostics" library
Imports System.Diagnostics

Class HiddenConsole
Shared Sub Main()
' Application path and command line arguments
Dim ApplicationPath As String = "C:WINDOWSsystem32arp"
Dim ApplicationArguments As String = "-a"

' Create a new process object
Dim ProcessObj As Process = New Process()

' StartInfo contains the startup information of
' the new process
ProcessObj.StartInfo.FileName = ApplicationPath
ProcessObj.StartInfo.Arguments = ApplicationArguments

' These two optional flags ensure that no DOS window
' appears
ProcessObj.StartInfo.UseShellExecute = False
ProcessObj.StartInfo.CreateNoWindow = True

' If this option is set the DOS window appears again :-/
'ProcessObj.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

' This ensures that you get the output from the DOS application
ProcessObj.StartInfo.RedirectStandardOutput = True

' Start the process
ProcessObj.Start()

' Wait that the process exits
ProcessObj.WaitForExit()

' Now read the output of the DOS application
System.Windows.Forms.MessageBox.Show(ProcessObj.StandardOutput.ReadToEnd())
End Sub
End Class
anders December 28, 2009 at 16:32
The simple way to get a silent start of an exe file is to change the output type of the project from "Console Application" to "Windows Application". No form needed.

I use VS2008 and there it works.

The snippet can be used if you have an existing program or really need to redirect the output.
david December 28, 2009 at 11:38
This works great if the launcher of the program is already running.

It does not solve the problem of running silently via a double-click on a '.exe' file.

The only way I can think of doing that is to run as a Windows application but mark the initial form as 'invisible'.

Any other alternative?
Jose November 18, 2009 at 13:22
Exactly what i was searching for.
Good job.
Thank you very much.
abhinav August 17, 2009 at 17:36
Hi,

When i run this code, i get the unhandled exception:
"Win32Exception was unhandled."
This error occurs at the line:
ProcessObj.Start();
I tried to find a solution for this Win32 Exception, but could not find a good answer for that.
Does anybody has any idea about how to solve this out?

Thanks,

Abhinav
John January 21, 2009 at 23:40
Hello,

I have published a console application in C# and created the URL for the client to install the application from using the Publish Wizard within Visual C# Express. On installation, the console application automatically runs, is there a way for this to be configured not to run on installation?

Thanks,
John
new December 29, 2008 at 16:34
i want java code
Masterful December 28, 2008 at 14:56
(just make sure to put the backslash in there to escape the newline - it either got filtered out or I forgot to type it)
Masterful December 28, 2008 at 14:55
@Anish: Wouldn't
Result.Split(new char[] { 'n'}, StringSplitOptions.RemoveEmptyEntries); work?
Anish December 17, 2008 at 12:15
how can i read the output line by line so that i can put the values into a databse