开发者

Why won't streamreader read the standard error stream?

So, I got a piece of advice on debugging my code, but my attempt to debug has sprouted up more errors. I have all the correct usings, but here's my issue:

Process p = new Process();
p.StartInfo.FileName = "javac";
Directory.CreateDirectory(System.IO.Path.Combine(Application.StartupPath + @"\TempJavaalfgwaepfgawe"));
p.StartInfo.A开发者_如何学编程rguments = "-d " + System.IO.Path.Combine(Application.StartupPath + @"\TempJavaalfgwaepfgawe") + " " + files;
p.Start();

p.WaitForExit();
StreamReader sr = new StreamReader(p.StandardError);
MessageBox.Show(sr.ReadToEnd());

Anyhow, the error stems from when I declare the streamreader:

StreamReader sr = new StreamReader(p.StandardError);

I get the following two errors:

The best overloaded method match for 'System.IO.StreamReader.StreamReader(string)' has some invalid arguments

Argument 1: cannot convert from 'System.IO.StreamReader' to 'string'


The StandardError property of Process is already a StreamReader. There is no need to create a new one.

You do however need to redirect standard error before the process starts in order to read from it.

p.StartInfo.RedirectStandardError = true;


The p.StandardError property is already a StreamReader, so you can simply read it:

p.WaitForExit();
MessageBox.Show(p.StandardError.ReadToEnd());

Also don't forget to redirect the standard error before starting the process or youwill get an exception when you try to read it:

p.StartInfo.RedirectStandardError = true;


p.StandardError 

is already a StreamReader, use that instance.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜