Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
Could any tell me how do I run my batch file silently without command prompt?

I have 2 batch files, mybatchfile1 and mybatchfile2. i am calling mybatchfile1 from code and mybatchfile2 from mybatchfile1.

I have tried following code but it not working.


C#
Process process = new Process();
process.StartInfo.FileName = processInfo.FileName; //(mybatchFile.bat)
process.StartInfo.WorkingDirectory = processInfo.WorkingDirectory;
process.StartInfo.UserName = processInfo.UserName;
process.StartInfo.Password = processInfo.Password;
process.StartInfo.Arguments = processInfo.Arguments;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.UseShellExecute = false;
process.Start();
Posted

it looks ok - you dont explain why its not working or what you see so its a bit hard for us to help

you could try redirecting standard output/error with something like

C#
process.StartInfo.RedirectStandardOutput = true;
process.OutputDataReceived += new DataReceivedEventHandler(ProcessOutputHandler);
process.StartInfo.RedirectStandardError = true;
process.ErrorDataReceived += new DataReceivedEventHandler(ProcessOutputHandler);


and defining a handler

C#
private void ProcessOutputHandler(object sendingProcess, DataReceivedEventArgs stdOutErrorLine)
{
   if (!String.IsNullOrEmpty(stdOutErrorLine.Data))
   {
        // Use stdOutErrorLine.Data or not 
   }
}
 
Share this answer
 
Comments
vishal.shimpi 14-Oct-15 7:29am    
I have already tried that, but still it didn't work for me. I don't know why its not working. My scenario is like I am calling 1st batch file from code and another (2nd) batch file from 1st one. I have tried to call batch1 file from .vbs. It suppress the command window but wont execute 2nd batch file.
Got the answer why Command window is not suppressing while executing batch file.

It is clearly mentioned in msdn site that : If the UseShellExecute property is true or the UserName and Password properties are not null, the CreateNoWindow property value is ignored and a new window is created.

Visit following URL : https://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.createnowindow.aspx[^]
 
Share this answer
 

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