2 Replies - 3900 Views - Last Post: 14 October 2010 - 04:28 AM Rate Topic: -----

#1 guy_tal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 14-October 10

Redirection of .exe file output in GUI application in C#

Posted 14 October 2010 - 03:21 AM

Hello,

I work with C# and I want to execute .exe file and I want to redirect the output of the command, so I could put the output in string variable.

I try the following code in Console application and it works great - the output of the command will be in "out" variable:

Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "C:\\dcmdump.exe";
p.StartInfo.Arguments = "C:\\IMG.dcm";
p.Start();
string out = p.StandardOutput.ReadToEnd();
p.WaitForExit();


But when I try to execute exactly the same code in a simple GUI application it doesn't work - the "out" variable will contain an empty string :(

What can be the problem?

Thanks a lot!

Is This A Good Question/Topic? 0
  • +

Replies To: Redirection of .exe file output in GUI application in C#

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,496
  • Joined: 23-August 08

Re: Redirection of .exe file output in GUI application in C#

Posted 14 October 2010 - 04:08 AM

I have this link saved specifically for this purpose.
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5804
  • View blog
  • Posts: 12,643
  • Joined: 16-October 07

Re: Redirection of .exe file output in GUI application in C#

Posted 14 October 2010 - 04:28 AM

Code looks fine. I did a proof of concept test with this:
Process GetTestProcess() {
	Process p = new Process();
	p.StartInfo.UseShellExecute = false;
	p.StartInfo.RedirectStandardOutput = true;
	p.StartInfo.FileName = "cmd.exe";
	p.StartInfo.Arguments = "/c dir";
	return p;
}

void ShowOutput1() {
	Process p = GetTestProcess();
	p.Start();
	string output = p.StandardOutput.ReadToEnd();
	p.WaitForExit();
	Debug.WriteLine("StandardOutput Begin");
	Debug.WriteLine(output);
	Debug.WriteLine("StandardOutput End");
}



However, there's another way to hook into the stream, shown here: http://msdn.microsof...utreadline.aspx

This one also worked and may be more suitable for what you're doing.
void ShowOutput2() {
	int lineNum = 0;
	StringBuilder sb = new StringBuilder();
	Process p = GetTestProcess();
	p.OutputDataReceived += new DataReceivedEventHandler(
		delegate(object sender, DataReceivedEventArgs args) {
			Debug.WriteLine("Process line: " + (++lineNum));
			sb.Append(args.Data + "\n");
		}
		);

	p.Start();
	p.BeginOutputReadLine();
	p.WaitForExit();
	Debug.WriteLine("Results Begin");
	Debug.WriteLine(sb.ToString());
	Debug.WriteLine("Results End");
}



Don't forget there's also an error stream. Hope this helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1