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();
Author:
Jonas John
License:
Public domain
Language:
C#
Created:
08/22/2007
Updated:
08/22/2007
Tags:
example, console, processes
Related links:
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:
thank you
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.
plzzzz rectify my problems....
Thanks...
'%SystemRoot%/Microsoft.NET/Framework/v2.0.50727/vbc.exe %buildParam%
Dim ApplicationPath As String = "C:/WINDOWS/system32/arp.exe"
'#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
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.
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?
Good job.
Thank you very much.
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
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
Result.Split(new char[] { 'n'}, StringSplitOptions.RemoveEmptyEntries); work?