【发布时间】:2012-02-08 04:48:13
【问题描述】:
我目前在重定向 STDError 和 STDOutput 时遇到问题。我想要做的是,如果没有抛出错误,则将错误与正常输出一起打印到富文本框。
我遇到的问题是,如果我添加该行来重定向 SDT 错误:
string error = process1.StandardError.ReadToEnd();
rchsdtOut.Text = error;
然后我的正常 STD Out 不会重定向到文本字段,但如果有打印错误。
process1 = new System.Diagnostics.Process();
process1.StartInfo.UseShellExecute = false;
process1.StartInfo.RedirectStandardOutput = true;
process1.StartInfo.RedirectStandardError = true;
process1.StartInfo.CreateNoWindow = true;
process1.StartInfo.FileName = "java.exe ";
//String abc = txtSingleBar.Text.Replace("\\", "/");
toLoad = lstBarToLoad.Items[i].Text;
process1.StartInfo.Arguments = "-Xmx512M -jar";
process1.StartInfo.Arguments += toLoad;
Console.WriteLine(process1.StartInfo.Arguments);
try
{
process1.Start();
process1.OutputDataReceived += (s, a) => myMethod(a);
process1.BeginOutputReadLine();
string error = process1.StandardError.ReadToEnd();
rchsdtOut.Text = error;
}
将事件写入文本字段的方法
private void myMethod(DataReceivedEventArgs e)
{
if (e.Data != null)
{
Action action = () => rchsdtOut.Text += "\r\n" + e.Data.ToString();
rchsdtOut.BeginInvoke(action, null);
Console.WriteLine(e.Data.ToString());
}
}//end of private
基本上我想要的是被重定向,如果应该发生 SDTOut 和 SDTError。
有什么想法吗?
【问题讨论】:
-
您发布的代码似乎存在语法错误。请发布可以编译的代码。
-
不确定你的意思。它符合我的要求。