Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide cmd window while running a batch file?

Tags:

How to hide cmd window while running a batch file?

I use the following code to run batch file

process = new Process(); process.StartInfo.FileName = batchFilePath; process.Start(); 
like image 259
Ahmed Atia Avatar asked Jul 08 '09 07:07

Ahmed Atia


People also ask

How do I hide a batch window?

Perhaps the simplest solution to covering a bay window is to treat the entire area as one space. Installing a curtain rod across the opening and then hanging sheer, floor-length curtains over it can keep your space light while blocking off any unsightly features.


2 Answers

If proc.StartInfo.UseShellExecute is false, then you are launching the process and can use:

proc.StartInfo.CreateNoWindow = true; 

If proc.StartInfo.UseShellExecute is true, then the OS is launching the process and you have to provide a "hint" to the process via:

proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; 

However the called application may ignore this latter request.

If using UseShellExecute = false, you might want to consider redirecting standard output/error, to capture any logging produced:

proc.StartInfo.UseShellExecute = false; proc.StartInfo.RedirectStandardOutput = true; proc.OutputDataReceived += new DataReceivedEventHandler(ProcessOutputHandler); proc.StartInfo.RedirectStandardError = true; proc.ErrorDataReceived += new DataReceivedEventHandler(ProcessOutputHandler); 

And have a function like

private void ProcessOutputHandler(object sendingProcess, DataReceivedEventArgs outLine) {    if (!String.IsNullOrEmpty(outLine.Data)) // use the output outLine.Data somehow; } 

There's a good page covering CreateNoWindow this on an MSDN blog.

There is also a bug in Windows which may throw a dialog and defeat CreateNoWindow if you are passing a username/password. For details

http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=98476 http://support.microsoft.com/?kbid=818858

like image 74
Joel Goodwin Avatar answered Oct 10 '22 23:10

Joel Goodwin


According to the Process properties, you do have a:

Property: CreateNoWindow
Notes: Allows you to run a command line program silently. It does not flash a console window.

and:

Property: WindowStyle
Notes: Use this to set windows as hidden. The author has used ProcessWindowStyle.Hidden often.

As an example!

static void LaunchCommandLineApp() {     // For the example     const string ex1 = "C:\\";     const string ex2 = "C:\\Dir";      // Use ProcessStartInfo class     ProcessStartInfo startInfo = new ProcessStartInfo();     startInfo.CreateNoWindow = false;     startInfo.UseShellExecute = false;     startInfo.FileName = "dcm2jpg.exe";     startInfo.WindowStyle = ProcessWindowStyle.Hidden;     startInfo.Arguments = "-f j -o \"" + ex1 + "\" -z 1.0 -s y " + ex2;      try     {         // Start the process with the info we specified.         // Call WaitForExit and then the using statement will close.         using (Process exeProcess = Process.Start(startInfo))         {             exeProcess.WaitForExit();         }     }     catch     {         // Log error.     } } 
like image 31
VonC Avatar answered Oct 11 '22 00:10

VonC