Redirect Cygwin Bash StandardInput, StandardOutput and StandardError

P

Panagiotis

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
capture,
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
directory.

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
behaviour?

thank you,

Gioti


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 =
"j:/cygwin/cygwin-1.5.18-1-3/bin/bash.exe"";
proc.StartInfo.Arguments = "-i -l";

proc.Start();

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

sw.AutoFlush = true;

cmd = "ls -lh";
sw.WriteLine(cmd);

while(true)
{
//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());
}
}

sw.Close();
sr.Close();

proc.WaitForExit();
proc.Close();
}
 
P

Panagiotis

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
to:
- Microsoft Visual Studio 2005
- version 2.0 of the .NET Framework

and now the code works like a charm ...

cu,

Panagiotis Arkoudopoulos
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top