1. <small id='ZpVc3'></small><noframes id='ZpVc3'>

      • <bdo id='ZpVc3'></bdo><ul id='ZpVc3'></ul>

      <i id='ZpVc3'><tr id='ZpVc3'><dt id='ZpVc3'><q id='ZpVc3'><span id='ZpVc3'><b id='ZpVc3'><form id='ZpVc3'><ins id='ZpVc3'></ins><ul id='ZpVc3'></ul><sub id='ZpVc3'></sub></form><legend id='ZpVc3'></legend><bdo id='ZpVc3'><pre id='ZpVc3'><center id='ZpVc3'></center></pre></bdo></b><th id='ZpVc3'></th></span></q></dt></tr></i><div id='ZpVc3'><tfoot id='ZpVc3'></tfoot><dl id='ZpVc3'><fieldset id='ZpVc3'></fieldset></dl></div>
      <tfoot id='ZpVc3'></tfoot><legend id='ZpVc3'><style id='ZpVc3'><dir id='ZpVc3'><q id='ZpVc3'></q></dir></style></legend>

      重定向 cmd.exe 的输入和输出

      时间:2023-08-27
    1. <small id='JpCEh'></small><noframes id='JpCEh'>

        <tfoot id='JpCEh'></tfoot>

        • <legend id='JpCEh'><style id='JpCEh'><dir id='JpCEh'><q id='JpCEh'></q></dir></style></legend>

            • <i id='JpCEh'><tr id='JpCEh'><dt id='JpCEh'><q id='JpCEh'><span id='JpCEh'><b id='JpCEh'><form id='JpCEh'><ins id='JpCEh'></ins><ul id='JpCEh'></ul><sub id='JpCEh'></sub></form><legend id='JpCEh'></legend><bdo id='JpCEh'><pre id='JpCEh'><center id='JpCEh'></center></pre></bdo></b><th id='JpCEh'></th></span></q></dt></tr></i><div id='JpCEh'><tfoot id='JpCEh'></tfoot><dl id='JpCEh'><fieldset id='JpCEh'></fieldset></dl></div>

              • <bdo id='JpCEh'></bdo><ul id='JpCEh'></ul>
                  <tbody id='JpCEh'></tbody>
              • 本文介绍了重定向 cmd.exe 的输入和输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我想将 cmd.exe 输出重定向到某处,当命令为一行时,下面的代码可以工作:

                I wanna redirect cmd.exe output somewhere, below code works when the command is a line:

                Process p = new Process()
                {
                    StartInfo = new ProcessStartInfo("cmd")
                    {
                       UseShellExecute = false,
                       RedirectStandardInput = true,
                       RedirectStandardOutput = true,
                       CreateNoWindow = true,
                       Arguments = String.Format("/c "{0}"", command),
                    }
                };
                p.OutputDataReceived += (s, e) => Messagebox.Show(e.Data);
                p.Start();
                p.BeginOutputReadLine();
                p.WaitForExit();
                

                但是像 WriteLine() 这样的一系列命令怎么样:

                But how about a series commands like WriteLine():

                p.StandardInput.WriteLine("cd...");
                p.StandardInput.WriteLine("dir");
                

                在这种情况下如何获得输出?

                how to get output in this situation?

                推荐答案

                要实现这样的行为,您应该使用 /k 开关以交互模式运行 cmd.exe.

                To achieve such behavior you should use /k switch to run cmd.exe in interactive mode.

                问题在于将输入与不同的命令分开.为此,您可以使用 prompt 命令更改标准提示:

                The problem is to separate inputs from different commands. To do this you could change the standard prompt using prompt command:

                prompt --Prompt_C2BCE8F8E2C24403A71CA4B7F7521F5B_F659E9F3F8574A72BE92206596C423D5 
                

                所以现在很容易确定命令输出的结束.

                So now it is pretty easy to determine the end of command output.

                完整代码如下:

                public static IEnumerable<string> RunCommands(params string[] commands) {
                    var process = new Process {
                        StartInfo = new ProcessStartInfo("cmd") {
                            UseShellExecute = false,
                            RedirectStandardInput = true,
                            RedirectStandardOutput = true,
                            CreateNoWindow = true,
                            Arguments = "/k",
                        }
                    };
                
                    process.Start();
                
                    const string prompt = "--Prompt_C2BCE8F8E2C24403A71CA4B7F7521F5B_F659E9F3F8574A72BE92206596C423D5 ";
                
                    // replacing standard prompt in order to determine end of command output
                    process.StandardInput.WriteLine("prompt " + prompt);
                    process.StandardInput.Flush();
                    process.StandardOutput.ReadLine();
                    process.StandardOutput.ReadLine();
                
                    var result = new List<string>();
                
                    try {
                        var commandResult = new StringBuilder();
                
                        foreach (var command in commands) {
                            process.StandardInput.WriteLine(command);
                            process.StandardInput.WriteLine();
                            process.StandardInput.Flush();
                
                            process.StandardOutput.ReadLine();
                
                            while (true) {
                                var line = process.StandardOutput.ReadLine();
                
                                if (line == prompt) // end of command output
                                    break;
                
                                commandResult.AppendLine(line);
                            }
                
                            result.Add(commandResult.ToString());
                
                            commandResult.Clear();
                
                        }
                    } finally {
                        process.Kill();
                    }
                
                    return result;
                }
                

                它运作良好,但看起来像一个大黑客.

                It works well but it looks like one big hack.

                我建议您改为使用每个命令的进程.

                I'd recommend you to use process per command instead.

                这篇关于重定向 cmd.exe 的输入和输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:C#:以隐藏方式运行外部控制台程序 下一篇:TimeSeries 趋势数据的重采样、聚合和插值

                相关文章

                最新文章

              • <tfoot id='OlZ3k'></tfoot>
              • <small id='OlZ3k'></small><noframes id='OlZ3k'>

                <legend id='OlZ3k'><style id='OlZ3k'><dir id='OlZ3k'><q id='OlZ3k'></q></dir></style></legend>

                    • <bdo id='OlZ3k'></bdo><ul id='OlZ3k'></ul>
                    <i id='OlZ3k'><tr id='OlZ3k'><dt id='OlZ3k'><q id='OlZ3k'><span id='OlZ3k'><b id='OlZ3k'><form id='OlZ3k'><ins id='OlZ3k'></ins><ul id='OlZ3k'></ul><sub id='OlZ3k'></sub></form><legend id='OlZ3k'></legend><bdo id='OlZ3k'><pre id='OlZ3k'><center id='OlZ3k'></center></pre></bdo></b><th id='OlZ3k'></th></span></q></dt></tr></i><div id='OlZ3k'><tfoot id='OlZ3k'></tfoot><dl id='OlZ3k'><fieldset id='OlZ3k'></fieldset></dl></div>