0

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; ?

jeremychan
  • 4,309
  • 10
  • 42
  • 56
  • 1
    possible duplicate of [How do I use ProcessStartInfo to run a batch file?](http://stackoverflow.com/questions/2382683/how-do-i-use-processstartinfo-to-run-a-batch-file) – Merlyn Morgan-Graham Mar 23 '11 at 08:18
  • Doesn't answer your question but just FYI there's some batch running info available on another [Stack Overflow answer](http://stackoverflow.com/questions/1096591/how-to-hide-cmd-window-while-running-a-batch-file/1096626#1096626). – Joel Goodwin Mar 23 '11 at 08:38

2 Answers2

5

UseShellExecute = true should do it.

Alternatively, if you need redirection, use:

runantc.StartInfo.FileName = "CMD.EXE";
runantc.StartInfo.Arguments = "/C " + Antcbatchpath;
Erik
  • 88,732
  • 13
  • 198
  • 189
  • hi erik i need to direct it to write a text file for the error and outputs. can i still set useshellexecute to true? – jeremychan Mar 23 '11 at 08:15
  • Tells CMD to close after executing the batch file. You could use /K to execute the batch file but not exit CMD afterwards, but you need to pass either /C or /K to have the batch file executed. – Erik Mar 23 '11 at 08:23
  • i have a CMD.EXE & antc.bat in my C:\GUI\ so i should just use `runantc.StartInfo.FileName = "CMD.EXE"` and `runantc.StartInfo.Arguments = "/C " + Antcbatchpath` – jeremychan Mar 23 '11 at 08:24
  • @jeremychan: yes. `CMD.EXE /C C:\GUI\antc.bat` would be the command, and that should work fine. – Erik Mar 23 '11 at 08:28
  • @erik i dont know why but even after i done that it is still not working. I am using wpf by the way. And using my orignal code as shown in my question, i dragged my application into the C:\GUI and it worked perfectly – jeremychan Mar 23 '11 at 09:08
  • @jeremychan: Replace \ with \\ in your string – Erik Mar 23 '11 at 09:11
  • hi erik, i think the problem lies with my white spaces. i dont think replacing \ with \\ in my string helps.. because i already put an "@" infront of the string. please clarify if i am wrong. thanks – jeremychan Mar 24 '11 at 03:06
  • oh so i have to add additional `'`? – jeremychan Mar 24 '11 at 07:40
  • @jeremychan: Yes, " or ' so that CMD sees your bat file name as one parameter. – Erik Mar 24 '11 at 07:41
0

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.

Fun Mun Pieng
  • 6,751
  • 3
  • 28
  • 30