【问题标题】:Redirecting Command output to textbox [duplicate]将命令输出重定向到文本框[重复]
【发布时间】:2017-04-24 02:22:33
【问题描述】:

我在 WinForms 应用程序中运行 sDelete64.exe。我将它的输出重定向到一个文本框。但是,只有输出的第一行被重定向。如果你正常运行,sDelete 显示的是

sDelete is set for 1 pass.
Zeroing free space on C:\: 0%

当我重定向它时,我只得到第一行。如果我重定向 dir 之类的命令,没问题,我可以恢复所有内容。

    private bool ExecuteCmd(string command)
    {
        var startInfo = new ProcessStartInfo("cmd.exe")
        {
            RedirectStandardInput = true,
            RedirectStandardOutput = true,
            RedirectStandardError = true,
            UseShellExecute = false,
            CreateNoWindow = true,
            Arguments = "/c " + command
        };


        _process = new Process { StartInfo = startInfo };
        Logger.Debug(string.Format("Cmd.exe is Launching command {0}", command));
        _process.Start();

        _process.OutputDataReceived += (o, e) =>
        {
            if (!string.IsNullOrEmpty(e.Data))
            {
                Logger.Debug("Updating loading panel with {0}", e.Data);
                _textBox.Text += e.Data + "\r\n";

            }

        };

        _process.ErrorDataReceived += (o, e) =>
        {
            if (!string.IsNullOrEmpty(e.Data))
            {
                Logger.Debug("Updating loading panel with {0}", e.Data);
                _textBox.Text += e.Data + "\r\n";

            }

        };

        _process.BeginErrorReadLine();

        _process.BeginOutputReadLine();

        _process.WaitForExit();

        return _process.HasExited;

什么可能导致 sDelete 的 StandardOutput 未被捕获?

【问题讨论】:

  • 首先,在分配事件处理程序之前调用 Start() 似乎很奇怪。不确定这里是否重要......

标签: c# winforms


【解决方案1】:

真的,我在这里找到了答案。创建一个将持续从 StandardOutput Stream 读取并更新 StreamWriter 的进程。

C# - Realtime console output redirection

【讨论】:

    猜你喜欢
    • 2018-12-09
    • 2020-10-14
    • 2018-06-07
    • 2012-11-15
    • 1970-01-01
    • 1970-01-01
    • 2015-10-30
    • 1970-01-01
    • 2018-07-23
    相关资源
    最近更新 更多