将批处理 shell 脚本输出读入 C# .Net 程序 [英] Reading Batch shell script output into C# .Net Program
问题描述
对于一个项目,我正在为旧的批处理脚本系统构建一个新的前端.我必须在 .Net 中使用 Windows XP 和 C#.我不想碰这个旧的后端系统,因为它是在过去十年中精心打造的.所以我的想法是启动 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.
非常感谢!
推荐答案
你的方法很好.但是最后你只能得到整个输出.我想要脚本运行时的输出.所以在这里,首先几乎相同,但比我对输出进行了调整.如果您有问题,请查看: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屋!