tags:

views:

621

answers:

4

I want to execute a BAT file through the use of C# code.

I attempted to use the following code,

 Process aProcess = new Process();
 aProcess = Process.Start(@"E:\IMP_DATA\PRC_Helper_uTest.bat");
 aProcess.WaitForExit(24000);
 aProcess.Close();

It starts the batch file but very next second stops.

I am not able to see any thing.

Can anyone help me regarding this problem?

UPDATE

Actually I want to start a new command prompt and run a batch file on that newly created command prompt.

How could I achieve this?

+3  A: 

Add PAUSE in the end of .bat file?

Dmitry Lobanov
Did that work, Nihkil?
Steven Sudit
+5  A: 

Here is some code that might make a difference , set the process not to launch a shell using "aProcess.UseShellExecute = false;" and then redirect the output to a stream using

 aProcess.RedirectStandardError = true;
 aProcess.RedirectStandardOutput = true;
 string Results = aProcess.StandardOutput.ReadToEnd();

This should return the output that your batch file would show

RC1140
If you don't launch a shell, I don't see how the batch will load at all.
Steven Sudit
The process is still executed in the background , you will just not see the output , which is why the redirects are used. This is useful in automated process.
RC1140
But it's a batch file, so it can't be executed by the OS directly, the way it would executer an EXE. Instead, we launch the shell and allow it to interpret the batch, right?
Steven Sudit
cmd.exe would be launched, and while hidden, redirect the output to the StandardOutput stream.
Dykam
**Actually i want to start a new command promt and wants to run a batch file on that newly created command promt. **
Nikhil, you're fine. The default behavior of the Process class is to launch cmd.exe, so things would occur as Dykam describes.
Steven Sudit
A: 

Do you want the command prompt to stay open after the batch file has been executed? If so, then start "cmd.exe" with your batch file as argument, otherwise just put a pause command at the end of the batch file as it was said already before.

Best Regards

Oliver Hanappi
That i added, But as i updated my question, I have to start a new command promt through bat file, and run a command on that newly created command promt.But what's happing, Program starting a new command promt but another command its running on the .NET cell.Please see the code and bat file: Bat FileX:cd \syngoBASE\i586-WinNT5.01\Debug\DoBin\binsyngo.Common.Starter.exe -shell dirpauseCode Process aProcess = new Process(); aProcess = Process.Start(@"E:\IMP_DATA\Test.bat"); aProcess.WaitForExit(24000); aProcess.Close();
Nikhil, if the batch file starts another process, the first one may well return before the second one has completed.
Steven Sudit
A: 

As you wrote in your update, you want to start a new command prompt and start a batch file on this prompt. But what you did, was starting a batch file.

So to get this to work, start a command prompt, executing your batch file instead of execute your batch file (Sometimes it can be so simple ;-)).

Example:

private void StartCmdWithBatch(string nameOfBatchFile)
{
    if (!File.Exists(nameOfBatchFile))
     throw new FileNotFoundException(nameOfBatchFile);

    string parameters = String.Format("/k \"{0}\"", nameOfBatchFile);
    System.Diagnostics.Process.Start("cmd", parameters);
}

Further informations about command prompt options can be get by cmd /?

Oliver