这是windows-application-form的代码;我想要执行的批处理文件显示我通过RedirectStandardOutput = false;
得到的shell屏幕上的输出,但我也希望输出被同时重定向到日志文件。为此,我使用RedirectStandardOutput = true;
。如何重定向输出以及同时获得shell屏幕上的输出
当然,一次只能使用一个!
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "c:\test\build.bat";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true; // if I use false all the commented lines below are not applicable for comments
p.Start();
string output = null;
//try
//{
output = p.StandardOutput.ReadToEnd();
//}
//catch (Exception ex)
//{
// MessageBox.Show(ex.ToString());
//}
System.IO.File.WriteAllText("c:\test\log.txt", output);
这不是很有帮助,他/她显然是在使用Windows,因为他们正在写C:\ – codemonkeh 2010-08-31 04:47:42
@codemonkeh:你认为我的第一句话本身更有用吗?我完全知道我们在这里与Windows打交道,这就是为什么我没有说“只使用'tee'”,但是该怎么做。事实上,在面向命令行的系统上执行POSIX标准化命令是一种常见的事情,这只是背景信息,有助于说服OP确信这是正确的方法。 – 2010-08-31 05:00:48
我认为@ system-rule正在寻找一种特定的代码内解决方案,所以他们更喜欢一个例子。对不起,如果我冒犯了你。 – codemonkeh 2010-08-31 05:22:24