Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to run a .bat file (actually a series of them) from a .NET application, and hide (or at least have minimised) the command window - but it will insist on flashing up. Tried with and without the commented out lines below.

Any ideas?

What I have tried:

VB
psInfo = New ProcessStartInfo(Path_to_bat_file)
psInfo.WorkingDirectory = Application.StartupPath & "\bats"
psInfo.WindowStyle = ProcessWindowStyle.Minimized
' psInfo.UseShellExecute = False
' psInfo.RedirectStandardInput = False
myProcess = Process.Start(psInfo)
myProcess.WaitForExit()
or, if you prefer:
C#
psInfo = new ProcessStartInfo(Path_to_bat_file);
psInfo.WorkingDirectory = Application.StartupPath + "\\bats";
psInfo.WindowStyle = ProcessWindowStyle.Minimized;
// psInfo.UseShellExecute = false;
// psInfo.RedirectStandardInput = false;
myProcess = Process.Start(psInfo);
myProcess.WaitForExit();
Posted
Updated 26-Nov-17 10:10am

1 solution

The RedirectStandardInput property defaults to false, hence commented or not the same applies.
Try as follows;
C#
// Create 2 stream readers to capture standard output & error if required
StreamReader srOut;
StreamReader srErr;
// create a new process
using(System.Diagnostics.Process proc = new System.Diagnostics.Process())
{
    proc.StartInfo.UseShellExecute = false;
    proc.StartInfo.RedirectStandardOutput = true;
    proc.StartInfo.RedirectStandardError = true;
    // Set command to run in a hidden window
    proc.StartInfo.CreateNoWindow = true;
    proc.StartInfo.FileName = "Path and Name of BAT file";
    proc.Start();
    // Set output streams if needed
    srOut = proc.StandardOutput;
    srErr = proc.StandardError;
    proc.WaitForExit();
}
// read output & error stream if required
string strRead = srOut.ReadToEnd();
string strError = srErr.ReadToEnd();


NOTE: If your Bat file can hang then you will need to terminate it programmatically - it will not be visible on the Desktop

MSDN ProcessStartInfo Class (System.Diagnostics)[^]

Kind Regards
 
Share this answer
 
Comments
A_Griffin 26-Nov-17 18:45pm    
Thank you so much!
an0ther1 26-Nov-17 20:42pm    
No problem, if the solution works please accept the solution

Kind Regards

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900