读取批处理shell脚本输出到C#.NET程序 [英] Reading Batch shell script output into C# .Net Program

查看:119
本文介绍了读取批处理shell脚本输出到C#.NET程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关项目我建立了一个古老的批处理脚本系统一个新的前端。我使用Windows XP和C#与.net。我不希望,因为它是制作在过去十年来接触这个古老的后端系统。所以我的想法是启动的cmd.exe 程序,并在那里执行Bash脚本。为此,我将使用.NET中的系统功能。

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.

但我还需要阅读批处理脚本命令行输出回到我的C#程序。我可以把它重定向到文件中。但是,必须有一种方式来获得从的CMD.exe 标准出在了我的C#程序。

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.

非常感谢你!

推荐答案

您的方式是好的。但你只能选在年底整体输出。我想要的脚本运行时的输出。所以这里首先是pretty大致相同,但比我twised输出。
如果你有问题,看一下:
 的http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.aspx

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...

的DoSomething的是这个功能:

The DoSomething is this function:

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

希望这有助于

这篇关于读取批处理shell脚本输出到C#.NET程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆