Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading Batch shell script output into C# .Net Program

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!

like image 779
Thomas Avatar asked Sep 18 '10 14:09

Thomas


People also ask

What is $1 and $2 in shell script?

$1 - The first argument sent to the script. $2 - The second argument sent to the script.

How do I run a shell script from a batch file?

You can allocate STDIN, STDOUT, and STDERR as z/OS UNIX files, using the PATH operand on the DD statements. You can also allocate STDOUT and STDERR as MVS data sets. Example: User TURBO runs a shell script in batch, as follows: The STDIN ddname defines a shell script to be invoked, /u/turbo/bin/myscript.sh.


2 Answers

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();
like image 68
Brian Rasmussen Avatar answered Oct 08 '22 14:10

Brian Rasmussen


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

like image 41
Thomas Avatar answered Oct 08 '22 16:10

Thomas