简体   繁体   中英

Launching a batch file

I have the following code:

String Antcbatchpath = @"C:\GUI\antc.bat";

System.Diagnostics.Process runantc = new System.Diagnostics.Process();
runantc.StartInfo.FileName = Antcbatchpath;
runantc.StartInfo.UseShellExecute = false;
runantc.StartInfo.RedirectStandardOutput = true;
runantc.StartInfo.RedirectStandardError = true;
runantc.Start();

Will this load the batch file from C:\\GUI\\antc.bat ?

Or runantc.StartInfo.FileName is only for a root directory? Root directory is where the application is located

EDIT 1:

hi instead of @"C:\\GUI\\antc.bat" i have a path:

String Antcbatchpath =@"C:\GUI Lab Tools\Build Machine\antc.bat";

which essentially contains white spaces. will it affect the runantc.StartInfo.Filename = Antcbatchpath; ?

UseShellExecute = true should do it.

Alternatively, if you need redirection, use:

runantc.StartInfo.FileName = "CMD.EXE";
runantc.StartInfo.Arguments = "/C " + Antcbatchpath;

You can try to set WorkingDirectory to prevent any ambiguity, but in my experience, it is not necessary.

The problem you're having is because antc.bat is not an executable. It requires UseShellExecute to be true, but that would prevent you from redirecting the output. I guess you will have to choose either one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM