阅读批次shell脚本输出成C#.NET PROGRAMM, [英] Reading Batch shell script output into C# .Net Programm,

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

问题描述

对于一个项目,我建立了一个旧的批处理脚本系统的一个新的前端。我使用Windows XP和C#与.net。我不想碰这个古老的后端系统,因为它的制作在过去的十年。所以我的想法是启动 CMD.EXE PROGRAMM,并在那里执行的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 Programm 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;
}

希望这有助于

Hope this Helps

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

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