Can't run batch file?

  • Thread starter Thread starter Brett Romero
  • Start date Start date
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
 
Hi,

The code seems correct ,

do you see at least a shell window?
 
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.
 
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
 
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
 
|
| 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.
 
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.
 
Is there a way to read the last three lines of output from the process
via StandardOutput or what ever is best?

Thanks,
Brett
 
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
 
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.
 
Back
Top