【问题标题】:Reading Batch shell script output into C# .Net Program将批处理 shell 脚本输出读入 C# .Net 程序
【发布时间】:2011-04-14 02:44:12
【问题描述】:

对于一个项目,我正在为旧的批处理脚本系统构建一个新的前端。我必须在 .Net 中使用 Windows XP 和 C#。我不想碰这个旧的后端系统,因为它是在过去十年中精心打造的。所以我的想法是启动cmd.exe 程序并在那里执行Bash 脚本。为此,我将使用 .Net 中的“系统”功能。

但我还需要将“批处理脚本命令行输出”读回到我的 C# 程序中。我可以将它重定向到一个文件中。但是必须有一种方法可以将来自CMD.exe 的标准输出导入到我的 C# 程序中。

非常感谢!

【问题讨论】:

  • Bash,你的意思是Batch吗?
  • @Brian:我认为 W_P 知道这一点。
  • @Dennis:可能是,但是 OP 确实多次编写了 bash 和 shell 脚本,所以我认为我们实际上是在谈论 bash。我想需要澄清一下。
  • 谢谢大家,改了make,它的windows cmd Batch,不是linux Bash脚本!
  • @Thomas:感谢您的澄清。请查看我的更新答案。

标签: c# .net windows batch-file cmd


【解决方案1】:

鉴于更新的问题。下面介绍如何启动 cmd.exe 以运行批处理文件并在 C# 应用程序中捕获脚本的输出。

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();

【讨论】:

    【解决方案2】:

    你的方法很好。但是你最后只能得到整个输出。我想要脚本运行时的输出。所以在这里,首先几乎相同,但我扭曲了输出。 如果您有问题请查看: 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;
    }
    

    有些不对劲,但不管你怎么想......

    DoSomething 就是这个函数:

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

    希望对你有帮助

    【讨论】:

    • BeginOutputReadLine() 也是需要的。
    • ^ 没有proc.BeginOutputReadLine() (和proc.BeginErrorReadLine() 用于错误数据),这将不起作用。
    【解决方案3】:

    您无法使用 system 函数捕获输出,您必须更深入地了解子流程 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*/ }
    

    您似乎对尝试运行 bash 脚本还是批处理文件感到困惑。这些不是一回事。 Bash 是一个 unix shell,存在多个 Windows 端口。 “批处理文件”是 cmd 脚本的常用名称。上面的代码假定您要运行 bash 脚本。如果要运行 cmd 脚本,请将 bash 更改为 cmd。如果您想运行 bash 脚本并且 bash.exe 不在您的 PATH 上,请将 bash 更改为 bash.exe 的完整路径。

    【讨论】:

    • 对不起,我以前说 bash 但我想要 Windows cmd Batch skript
    • 让你失望,你的代码运行良好。两位:在第 7 行有一个太多的括号。第二点:在 path_to_script 之后,您只需留下一条毯子,您就可以将您的参数添加到脚本中。非常感谢大家,我的老板很高兴:D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-11
    • 1970-01-01
    • 2013-01-05
    • 1970-01-01
    • 2015-09-08
    相关资源
    最近更新 更多