[Mono-list] Capturing output from Linux Command Line programs

mono-list.1.tracyanne at spamgourmet.com mono-list.1.tracyanne at spamgourmet.com
Tue Aug 16 21:55:52 EDT 2005


> Dilton McGowan II diltonm at yahoo.com 
> Tue Aug 16 19:38:50 EDT 2005
> 
> Using the code from your original post, substituting 
> ls for wget works. "standard" is not always standard.
> IIRC, some programs output to screen memory for speed
> rather than through the BIOS character IO subsystem.


So I have discovered, and that was what lead to my confusion. The
following code using wget does, however work. You will notice that I am
reading the stream from stderr. When I do this I am able to retrieve
the status information that wget normally writes to the console.

using System;
using System.Diagnostics;
using System.IO;

class MainClass
{
	public static void Main(string[] args)
	{
		ProcessStartInfo psi = new ProcessStartInfo();
		psi.FileName = "/usr/bin/wget";
		//
		//change the pths to one of your choosing
		//
		psi.Arguments = "-m -nd --directory-prefix=/home/tracy/Downloads/test --input-file=/home/tracy/Downloads/test/wgetlist";

		psi.RedirectStandardInput = false;
		psi.RedirectStandardOutput = false;
		psi.RedirectStandardError = true;
		
		//place the following text in a file name wgetlist
		//ftp://anonymous@ftp.planetmirror.com/disks/2/mandrake/devel/cooker/i586/media/main/cdrdao-1.2.0-2mdk.i586.rpm
		//
		
		psi.UseShellExecute = false;
		
		Process process = Process.Start(psi);
		
		Console.WriteLine("Process Name: " + process.ProcessName);
		Console.WriteLine("Process Id: " + process.Id);
		Console.WriteLine("Process RedirectInput: " + process.StartInfo.RedirectStandardInput);
		Console.WriteLine("Process RedirectOutput: " + process.StartInfo.RedirectStandardOutput);
		Console.WriteLine("Process RedirectError: " + process.StartInfo.RedirectStandardError);
		
		Console.WriteLine("Processing");			
		bool retn = false;
		while (!retn)
		{			
			StreamReader sr = null;
			sr = process.StandardError;

			string content = sr.ReadLine();
			Console.WriteLine("Stuff my GUI application will use: " + content);
			
			retn = process.WaitForExit(1000);
		}
		
		Console.WriteLine("Process has exited: " + retn.ToString());
		process.Close();
	}
}



More information about the Mono-list mailing list