By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,241 Members | 2,048 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,241 IT Pros & Developers. It's quick & easy.

Redirect Cygwin Bash StandardInput, StandardOutput and StandardError

P: n/a

Hi everyone,

this is what I want to do in C#:
1) start a Cygwin Bash Shell
2) send the Shell a command, for example "ls -l"
3) capture all the output of the command

The code below seems to work when I there isn't alot of output to
but when the output increases, the code will freeze.

For example: my "~/tmp" directory contains only 3 files.
Sending the command "ls -l ~/tmp" works as
expected, meaning that I am
able to capture the names of the files in the tmp

my "~/" directory contains 176 files.
Sending the command "ls -l ~" does not work as
expected, meaning that I am
able to capture the names of the first 164 files
after which the code freezes.

Does someone have an idea on what's causing this difference in

thank you,

public static void ExecuteCommand()
Process proc = new Process();
string stOut = "";

proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.FileName =
proc.StartInfo.Arguments = "-i -l";


StreamWriter sw = proc.StandardInput;
StreamReader sr = proc.StandardOutput;
StreamReader se = proc.StandardError;

sw.AutoFlush = true;

cmd = "ls -lh";

//if(sr.Peek() >= 0)
Console.WriteLine("sr.Peek = " + sr.Peek());
Console.WriteLine("sr = " + sr.ReadLine());

if(se.Peek() >= 0)
Console.WriteLine("se.Peek = " + se.Peek());
Console.WriteLine("se = " + se.ReadLine());



Dec 19 '05 #1
Share this Question
Share on Google+
1 Reply

P: n/a

Hi everyone,

it's working ... the problem was apparantly not in my code but
in my development environment.

I just switched my development environment from:
- Microsoft Visual Studio 2003
- version 1.1 of the .NET Framework
- Microsoft Visual Studio 2005
- version 2.0 of the .NET Framework

and now the code works like a charm ...


Panagiotis Arkoudopoulos

Dec 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.