Can't run batch file?

B

Brett Romero

I'd like to execute a batch file. However, I just get a quick DOS
window flash when I run this code:

System.Diagnostics.ProcessStartInfo p = new
System.Diagnostics.ProcessStartInfo
(@"C:\mybat.bat");
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = p;
proc.Start();
proc.WaitForExit();

If I double click the bat file, I see the DOS window with output and
everything executes. If I open a DOS window and type in the bat file,
everything works fine again. The file modifies other files. Looking
at the files that are supposed to be modified, I can see it isn't
executing via the C# code.

Any ideas?

Thanks,
Brett
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

The code seems correct ,

do you see at least a shell window?
 
B

Brett Romero

Ignacio said:
Hi,

The code seems correct ,

do you see at least a shell window?

No windows in other words. Just a flash but nothing stays around.

Brett
 
C

Chris Dunaway

Brett said:
No windows in other words. Just a flash but nothing stays around.

Brett


If you run your batch file manually, can it be invoked from anywhere?
Or must the command prompt be in a certain directory?

Try experimenting with the UseShellExecute and WorkingDirectory
properties of the ProcessStartInfo class. Perhaps setting them
appropriately will help.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

That is the expected behavior, the window stay while the program is
executing and disappear after it finish

try to put a ReadLine in the bat file

is it a short .bat ? if so post it here
 
B

Brett Romero

If you run your batch file manually, can it be invoked from anywhere?
Or must the command prompt be in a certain directory?

Try experimenting with the UseShellExecute and WorkingDirectory
properties of the ProcessStartInfo class. Perhaps setting them
appropriately will help.

Ah! Thanks Chris. A little modification:

System.Diagnostics.ProcessStartInfo p = new
System.Diagnostics.ProcessStartInfo( "mybat.bat" );
p.UseShellExecute = true;
p.WorkingDirectory = @"C:\";
p.WindowStyle = ProcessWindowStyle.Normal;

Thanks,
Brett
 
W

Willy Denoyette [MVP]

|
| Ignacio Machin ( .NET/ C# MVP ) wrote:
| > Hi,
| >
| > The code seems correct ,
| >
| > do you see at least a shell window?
|
| No windows in other words. Just a flash but nothing stays around.
|
| Brett
|

That means that the batch file gets started, right!, but questions are: what
are you doing in the bat file and why do you expect the cmd window to stay
arround?
Note that when one or another command in the bat fails, the command
interpreter will abort the curent bat file execution (unless you have some
error checking/recovery in place).

Willy.
 
S

Saad Rehmani

Try adding a 'pause' DOS command at the end of your batch file.

It should block the proc.WaitForExit() and allow you to see whats going on
during execution.
 
B

Brett Romero

Is there a way to read the last three lines of output from the process
via StandardOutput or what ever is best?

Thanks,
Brett
 
D

Dipankar

Hi,

You can try out this code.

System.Diagnostics.ProcessStartInfo p = new
System.Diagnostics.ProcessStartInfo(@"C:\mybat.bat");
p.UseShellExecute = true;
p.RedirectStandardOutput = true;
p.RedirectStandardError = true;

System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = p;
proc.Start();
proc.WaitForExit();

If you want to read the output from the console, can use this code as
well.
string output = proc.StandardOutput.ReadToEnd();

Cheers!!
Dipankar
 
C

Chris Dunaway

Brett said:
Is there a way to read the last three lines of output from the process
via StandardOutput or what ever is best?

Thanks,
Brett

Yes, you would have to set the RedirectStandardOutput property and then
open a stream against that to read what was output.
 

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