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

mono-list.1.tracyanne at spamgourmet.com mono-list.1.tracyanne at spamgourmet.com
Mon Aug 15 09:51:12 EDT 2005


I'm trying to work out how to capture stdout, and or pass data to stdin
of Linux Command line programs; for example wget.

I have the following code

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

class MainClass
{
	public static void Main(string[] args)
	{
		ProcessStartInfo psi = new ProcessStartInfo("wget");
		psi.Arguments = "-m -nd --directory-prefix=/home/tracy/Downloads/test
--input-file=/home/tracy/Downloads/test/wgetlist";
		psi.RedirectStandardOutput = true;
		psi.UseShellExecute = false;
		
		
		Process process = Process.Start(psi);
		bool retn = false;
		int i = 1;
		while (!retn)
		{
			Console.WriteLine(i.ToString());
			retn = process.WaitForExit(10);
			StreamReader sr = process.StandardOutput;			
			Console.WriteLine("#=> " + sr.ReadToEnd());
			i++;
		}
	}
}


which produces the following output.


--23:31:15--
ftp://anonymous@ftp.planetmirror.com/disks/2/mandrake/devel/cooker/i586/media/main/cdrdao-1.2.0-2mdk.i586.rpm
           => `/home/tracy/Downloads/test/.listing'
1
Resolving ftp.planetmirror.com... 203.16.234.85, 203.16.234.86
Connecting to ftp.planetmirror.com[203.16.234.85]:21... connected.
Logging in as anonymous ... Logged in!
==> SYST ... done.    ==> PWD ... done.
==> TYPE I ... done.  ==>
CWD /disks/2/mandrake/devel/cooker/i586/media/main ... done.
==> PASV ... done.    ==> LIST ... done.

    0K .......... .......... .......... .......... ..........    3.23
KB/s ??% 
   50K .......... .......... .......... .......... ..........   10.75
KB/s ??% 
  100K .......... .......... .......... .......... ..........   10.83
KB/s ??% 
  150K .......... .......... .......... .......... ..........    4.40
KB/s ??% 
  200K .......... .......... .......... .......... ..........    3.57
KB/s ??% 
  250K .......... .......... .......... .......... ..........   11.58
KB/s ??% 
  300K .......... .......... .......... .......... ..........   10.92
KB/s ??% 
  350K .......... ....                                        ??%
11.4K       

23:32:24 (6.05 KB/s) - `/home/tracy/Downloads/test/.listing' saved
[373,393]

Remote file no newer than local file
`/home/tracy/Downloads/test/cdrdao-1.2.0-2mdk.i586.rpm' -- not
retrieving.

FINISHED --23:32:24--
Downloaded: 373,393 bytes in 1 files
#=> 


>From this I can see that wget's stdout is not being redirected, and
strangely, even though I have a 10 milli second delay on WaitForExit,
WaitForExit seems to wait until wget has finished processing.

The "#=> " at the end of the output tells me that none of the output
comes from my program.


Can anyone show me where I have gone wrong.

Regards

Tracy Barlow


More information about the Mono-list mailing list