【发布时间】: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() 似乎很奇怪。不确定这里是否重要......