Call a batch file from C#

G

Gaurav

I am trying to call a .bat file from my C# windows form. I've tried:

System.Diagnostics.ProcessStartInfo p = new
System.Diagnostics.ProcessStartInfo(@"C:\gen-pdf.bat") ;
p.UseShellExecute=false;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo=p;
proc.Start();
proc.WaitForExit();

However, the batch file is not executing & the command window closes in a
flash.
Any ideas what I am missing?
 
J

Joe Cool

I am trying to call a .bat file from my C# windows form. I've tried:

System.Diagnostics.ProcessStartInfo p = new
System.Diagnostics.ProcessStartInfo(@"C:\gen-pdf.bat") ;
p.UseShellExecute=false;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo=p;
proc.Start();
proc.WaitForExit();

However, the batch file is not executing & the command window closes in a
flash.
Any ideas what I am missing?

I think you need to run cmd.exe and pass the name of the bat file as a
command line argument.
 
F

Fred Mellender

or, set p.UseShellExecute=true;

Also, after proc.WaitForExit(); have the statement:

proc.Close();

You could also put a statement:
pause

in your bat file to forestall the window closing so that you can see the
output and what is happening.


I am trying to call a .bat file from my C# windows form. I've tried:

System.Diagnostics.ProcessStartInfo p = new
System.Diagnostics.ProcessStartInfo(@"C:\gen-pdf.bat") ;
p.UseShellExecute=false;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo=p;
proc.Start();
proc.WaitForExit();

However, the batch file is not executing & the command window closes in a
flash.
Any ideas what I am missing?

I think you need to run cmd.exe and pass the name of the bat file as a
command line argument.
 
G

Gregory A. Beamer

However, the batch file is not executing & the command window closes
in a flash.
Any ideas what I am missing?

It is working fine for me. Perhaps you should capture the error message
and figure out what is wrong. To do this redirect standard error and
read standard error to end.

Most likely, you have something in the batch that is path dependent
(relative) rather than having an absolute path coded.

Peace and Grace,

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
M

Morten Wennevik [C# MVP]

Hi,

There are a couple of things that may be the cause of this.

1) The batch file executes in the application folder, not in the folder
where the batch file actually resides.

2) A security application may prevent cmd.exe from executing.

You can try to start cmd.exe and provide the batch file as an argument

ProcessStartInfo info = new ProcessStartInfo("cmd.exe", @"/C C:\gen-pdf.bat");
 
G

Gaurav

Thanks, but I still am not able to run the batch file from within my
application. I can however double click on the .bat file and it executes as
expected.

Here is the code inside the .bat file:
call fop-dir\fop -c fop-dir\fop.xconf -xml "C:\03-30.xml" -xsl test-fo.xsl
-pdf "C:\03-30.pdf" -param param1 "" -param param2 "" -param param3 ""

The batch file is placed on the same level as "fop-dir".
 
G

Gaurav

Never mind, I figured it out.
The application was trying to run the batch file from bin\debug, whereas the
actual location of the batch file was different.

Added a couple of cd commands at the top of the .bat file code to fix the
issue.
 
G

Gregory A. Beamer

Never mind, I figured it out.
The application was trying to run the batch file from bin\debug,
whereas the actual location of the batch file was different.

Added a couple of cd commands at the top of the .bat file code to fix
the issue.

Yes, by default, if you do not have an absolute path, it will try to run
from the /bin folder ... and fail. When you get into issues like this,
redirect the error output and standard output so you can view the errors.
The process object will catch them otherwise, and you will not see what is
going on.

Peace and Grace,

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top