0

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?

5 Answers 5

5

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

Sign up to request clarification or add additional context in comments.

6 Comments

If you don't launch a shell, I don't see how the batch will load at all.
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.
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?
cmd.exe would be launched, and while hidden, redirect the output to the StandardOutput stream.
**Actually i want to start a new command promt and wants to run a batch file on that newly created command promt. **
|
3

Add PAUSE in the end of .bat file?

Comments

1

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

Comments

0

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

2 Comments

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 File X: cd \syngoBASE\i586-WinNT5.01\Debug\DoBin\bin syngo.Common.Starter.exe -shell dir pause Code 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.
0

I used this bat file with two C# codes here it is:

@echo off

color 0a 

mode 1000

:a

echo %random% %random% %random% %random% %random% %random% %random% %random% 

goto a


Process aProcess = new Process();

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

aProcess.WaitForExit(24000);

aProcess.Close();


aProcess.RedirectStandardError = true;

aProcess.RedirectStandardOutput = true;

string Results = aProcess.StandardOutput.ReadToEnd();

And it worked perfectly fine.

Comments