我将控制台的输出保存到.txt文件时出现问题。有人可以帮我弄这个吗 ? txt文件中只有一行“输入输入”。就是这样。
ProcessStartInfo startInfo = new ProcessStartInfo("plink.exe");
startInfo.RedirectStandardError = true;
startInfo.Arguments = "@xxx.xxx.xxx.xxx -l xxx-pw xxxx";
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
Process process = new Process();
process.OutputDataReceived += (sender, e) =>
{
string Results = e.Data;
Console.WriteLine(Results);
//Console.WriteLine(e.Data);
};
FileStream ostrm;
StreamWriter writer;
TextWriter oldOut = Console.Out;
process.StartInfo = startInfo;
process.Start();
ostrm = new FileStream("D:/userdata/xxx/Desktop/Redirect.txt", FileMode.OpenOrCreate, FileAccess.Write);
writer = new StreamWriter(ostrm);
Console.WriteLine("Enter input:");
string line = Console.ReadLine();
Console.SetOut(writer);
process.BeginOutputReadLine();
process.StandardInput.WriteLine("c7xtermx -n xxxxxxx");
process.StandardInput.WriteLine("ZEEI:BCF=" + line + ";");
Console.SetOut(oldOut);
writer.Close();
ostrm.Close();
process.WaitForExit();
答案 0 :(得分:0)
进程可能在刷新所有数据之前尝试读取一行。将其添加到您的代码中,让我知道它是否有效:
writer.AutoFlush = true;