我需要在我的 c# 程序中执行此操作。

           Process process = LaunchCommandWindow("Special args and environment set")
           process.StandardInput.WriteLine("cmd1");
           process.StandardInput.WriteLine("cmd2");

           //Parse logs
            using (Streamreader sr =  new ())
           { etc etc..}

我的命令正确执行,但问题是 Parse 记录代码不会等待 cmd1 和 cmd2 执行完成。我不能调用退出和进程。 waitforexit 因为在解析日志后,我有更多的命令要执行。

以下是进程启动信息的设置方式。启动命令窗口()
         ProcessStartInfo startInfo = new ProcessStartInfo();

        startInfo.FileName = Path.Combine(Environment.SystemDirectory, "cmd.exe");
        startInfo.Arguments = "/K " + newEnvironmentArgs;

        startInfo.UseShellExecute = false;
        startInfo.RedirectStandardInput = true;
        Process process = new Process();
        process.StartInfo = startInfo;
        process.Start();

        return process;

如何获取解析日志代码以等待 cmd2 完成?

谢谢。

最佳答案

如果“cmd2”将完成写入标准输出流,您可以监听标准输出直到您看到该事件,然后从那里进行处理。

关于c# - 在 C# 中运行命令并等待完成,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4866836/

10-13 05:46