17

For a project I am building a new front end for an old Batch script System. I have to use Windows XP and C# with .Net. I don't want to touch this old Backend system as it's crafted over the past Decade. So my Idea is to start the cmd.exe Program and execute the Bash script in there. For this I will use the "system" function in .Net.

But I also need to read the "Batch script commandline Output" back into my C# Program. I could redirect it into a file. But there has to be a way to get the Standard Output from CMD.exe in into my C# Program.

Thank you very much!

5
  • 2
    by Bash, do you mean Batch? Commented Sep 18, 2010 at 14:49
  • @Brian: I think W_P knows that. Commented Sep 18, 2010 at 15:32
  • @Dennis: Could be, but the OP did write bash and shell script several times, so I take it we're actually talking about bash. I guess a clarification is in order. Commented Sep 18, 2010 at 15:39
  • thanks all , changed maked , its windows cmd Batch, not a linux Bash script! Commented Sep 18, 2010 at 17:04
  • possible duplicate of How To: Execute command line in C#, get STD OUT results Commented Sep 18, 2010 at 17:27

3 Answers 3

16

Given the updated question. Here's how you can launch cmd.exe to run a batch file and capture the output of the script in a C# application.

var process = new Process();
var startinfo = new ProcessStartInfo("cmd.exe", @"/C c:\tools\hello.bat");
startinfo.RedirectStandardOutput = true;
startinfo.UseShellExecute = false;
process.StartInfo = startinfo;
process.OutputDataReceived += (sender, args) => Console.WriteLine(args.Data); // do whatever processing you need to do in this handler
process.Start();
process.BeginOutputReadLine();
process.WaitForExit();
Sign up to request clarification or add additional context in comments.

Comments

6

Your ways are good. But you only get the whole Output at the end. I wanted the output when the script was running. So here it is, first pretty much the same, but than I twised the output. If you have problems look at: http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.aspx

public void execute(string workingDirectory, string command)
{   

    // create the ProcessStartInfo using "cmd" as the program to be run, and "/c " as the parameters.
    // Incidentally, /c tells cmd that we want it to execute the command that follows, and then exit.
    System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo(workingDirectory + "\\" + "my.bat ", command);

    procStartInfo.WorkingDirectory = workingDirectory;

    //This means that it will be redirected to the Process.StandardOutput StreamReader.
    procStartInfo.RedirectStandardOutput = true;
    //This means that it will be redirected to the Process.StandardError StreamReader. (same as StdOutput)
    procStartInfo.RedirectStandardError = true;

    procStartInfo.UseShellExecute = false;
    // Do not create the black window.
    procStartInfo.CreateNoWindow = true;
    // Now we create a process, assign its ProcessStartInfo and start it
    System.Diagnostics.Process proc = new System.Diagnostics.Process();

    //This is importend, else some Events will not fire!
     proc.EnableRaisingEvents = true;

    // passing the Startinfo to the process
    proc.StartInfo = procStartInfo;

    // The given Funktion will be raised if the Process wants to print an output to consol                    
    proc.OutputDataReceived += DoSomething;
    // Std Error
    proc.ErrorDataReceived += DoSomethingHorrible;
    // If Batch File is finished this Event will be raised
    proc.Exited += Exited;
}

Something is off, but whatever you get the idea...

The DoSomething is this function:

void DoSomething(object sendingProcess, DataReceivedEventArgs outLine);
{
   string current = outLine.Data;
}

Hope this Helps

2 Comments

BeginOutputReadLine() is also needed.
^ without proc.BeginOutputReadLine() (and proc.BeginErrorReadLine() for the error data), this will not work.
3

You can't capture the output with the system function, you have to go a bit deeper into the subprocess API.

using System;
using System.Diagnostics;
Process process = Process.Start(new ProcessStartInfo("bash", path_to_script) {
                                    UseShellExecute = false,
                                    RedirectStandardOutput = true
                                });
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
if (process.ExitCode != 0) { … /*the process exited with an error code*/ }

You seem to be confused as to whether you're trying to run a bash script or a batch file. These are not the same thing. Bash is a unix shell, for which several Windows ports exist. “Batch file” is the name commonly given to cmd scripts. The code above assumes that you want to run a bash script. If you want to run a cmd script, change bash to cmd. If you want to run a bash script and bash.exe is not on your PATH, change bash to the full path to bash.exe.

2 Comments

sorry, im used to say bash but i want Windows cmd Batch skript
tank you, your code is working nicely. Two bits: At line 7 there is an bracket to much. Second bit: after path_to_script you just leave a blanket and you can add your arguments to the script. Thank you all a lot , my boss is very happy :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.