我编写了一个进程,它从作为参数给定的文件中读取数据。我异步读取了StandardOutput,并同步读取了StandardError。
public static string ProcessScript(string command, string arguments)
{
Process proc = new Process();
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.FileName = command;
proc.StartInfo.Arguments = arguments;
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.Start();
string error = null;
string output = null;
proc.OutputDataReceived += (sender, outputLine) =>
{
if (outputLine.Data != null)
{
output += outputLine.Data;
}
};
proc.BeginOutputReadLine();
error = proc.StandardError.ReadToEnd();
proc.WaitForExit();
proc.Close();
//I have not got entire Output
return output;
}
在流程完成后我得到了输出,但并非全部。我只得到了部分数据。异步读取操作在进程完成任务后仍未结束,所以我只得到了部分数据。我需要完整的字符串。
编辑:
我正在使用 .Net 3.5。我不能使用ReadToEndAsync
方法。
有什么想法吗?