Redirection troubles

E

Edwin G. Castro

I want to start a process from a C# application. I also want to
redirect standard error to standard output so that I can read output
from both streams just like I could from a command line. In other
words, I want to emulate the following: command arg1 arg2 > file.txt
2>&1

I know I can get StreamReaders for standard error and standard output
separately, but I want them interleaved like in the sample command
line above. I want to be able to read from standard output and get
both what was written on standard output and standard error.

Any help would be appreciated!

Following is an example of what I want to do:
// <<<<start snippet>>>>
// NOTE: the argument string needs to have quote characters because
some
// arguments have spaces in them.
ProcessStartInfo p = new ProcessStartInfo("command", "\"ar g1\" \"ar
g2\"");
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;

// somehow setup standard error to go to standard output

Process p = new Process();
p.StartInfo = psi;
p.Start();

string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
// <<<<end snippet>>>>

Thanks!

--Edwin G. Castro
(e-mail address removed)
 
I

Ignacio Machin

Hi Edwin,

Did you try to instruct the program to redirect the standard error to
standard output as you mention?
I would play a little with this.
Like this:
ProcessStartInfo p = new ProcessStartInfo("command", "\"ar g1\" \"arg2\"
2>&1");


Hope this help.

Pd:
It may be a coincidence but I used to know a person with your name when I
was studing at Havana univ.
Cheers,
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi again,

Here is a solution:
ProcessStartInfo psi = new ProcessStartInfo(@"c:\winnt\system32\cmd.exe",
@"/c c:\test.exe 2>&1");

Cheers,

--
Ignacio Machin, ( .NET/ C# MVP )
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Ignacio Machin said:
Hi Edwin,

Did you try to instruct the program to redirect the standard error to
standard output as you mention?
I would play a little with this.
Like this:
ProcessStartInfo p = new ProcessStartInfo("command", "\"ar g1\" \"arg2\"
2>&1");


Hope this help.

Pd:
It may be a coincidence but I used to know a person with your name when I
was studing at Havana univ.
Cheers,
--
Ignacio Machin, ( MVP )
ignacio.machin AT [dot.state.fl.us]
Florida Department Of Transportation



Edwin G. Castro said:
I want to start a process from a C# application. I also want to
redirect standard error to standard output so that I can read output
from both streams just like I could from a command line. In other
words, I want to emulate the following: command arg1 arg2 > file.txt
2>&1

I know I can get StreamReaders for standard error and standard output
separately, but I want them interleaved like in the sample command
line above. I want to be able to read from standard output and get
both what was written on standard output and standard error.

Any help would be appreciated!

Following is an example of what I want to do:
// <<<<start snippet>>>>
// NOTE: the argument string needs to have quote characters because
some
// arguments have spaces in them.
ProcessStartInfo p = new ProcessStartInfo("command", "\"ar g1\" \"ar
g2\"");
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;

// somehow setup standard error to go to standard output

Process p = new Process();
p.StartInfo = psi;
p.Start();

string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
// <<<<end snippet>>>>

Thanks!

--Edwin G. Castro
(e-mail address removed)
 
E

Edwin G. Castro

Hi,

Thanks for the recommendation! Unfortunately, that route
doesn't work for me. The problem is with long pathnames
(with embedded spaces). In my case the path to C:\test.exe
would have spaces, C:\Some Directory\test.exe, forcing me
to use quote charaters. Unfortunately, some of the
parameters to the program I'm trying to run may have
spaces in them also. The cmd.exe interpreter has a tough
time dealing with more than one quoted string in it's
arguments. The result is that cmd.exe reports an error
saying that it can't find C:\Some. This is a limitation of
cmd.exe.

On the other hand, I did manage to fake redirecting
standard error to standard output. I realized that I was
only interested in the output one line at a time. So what
I'm doing is checking if there is anything in standard
output (using the Peek method) and reading one line if
there was. Then I check standard error in the same way. I
do this in an infinite loop until both streams are empty.
Here is some code:

// <<<<start snippet>>>>
while (true)
{
if (process.StandardOutput.Peek() >=0)
{
string line = process.StandardOutput.ReadLine();
// ...
}
else
{
if (process.StandardError.Peek() < 0) break;
}

if (process.StandardError.Peek() >= 0)
{
string line = process.StandardError.ReadLine();
// ...
}
else
{
if (process.StandardOutput.Peek() < 0) break;
}
}
// <<<<stop snippet>>>>

If this seems unreasonable please let me know. It is quite
possible that I haven't considered something.

Thanks for the input!

--Edwin G. Castro
(e-mail address removed)

PS - I'm from Puerto Rico; but now I live in Oregon. Is
Havana University in Florida or Cuba...?
-----Original Message-----
Hi again,

Here is a solution:
ProcessStartInfo psi = new ProcessStartInfo (@"c:\winnt\system32\cmd.exe",
@"/c c:\test.exe 2>&1");

Cheers,

--
Ignacio Machin, ( .NET/ C# MVP )
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


"Ignacio Machin" <ignacio.machin AT dot.state.fl.us> wrote in message
Hi Edwin,

Did you try to instruct the program to redirect the standard error to
standard output as you mention?
I would play a little with this.
Like this:
ProcessStartInfo p = new ProcessStartInfo ("command", "\"ar g1\" \"arg2\"
2>&1");


Hope this help.

Pd:
It may be a coincidence but I used to know a person with your name when I
was studing at Havana univ.
Cheers,
--
Ignacio Machin, ( MVP )
ignacio.machin AT [dot.state.fl.us]
Florida Department Of Transportation



Edwin G. Castro said:
I want to start a process from a C# application. I also want to
redirect standard error to standard output so that I can read output
from both streams just like I could from a command line. In other
words, I want to emulate the following: command arg1 arg2 > file.txt
2>&1

I know I can get StreamReaders for standard error and standard output
separately, but I want them interleaved like in the sample command
line above. I want to be able to read from standard output and get
both what was written on standard output and standard error.

Any help would be appreciated!

Following is an example of what I want to do:
// <<<<start snippet>>>>
// NOTE: the argument string needs to have quote characters because
some
// arguments have spaces in them.
ProcessStartInfo p = new ProcessStartInfo ("command", "\"ar g1\" \"ar
g2\"");
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;

// somehow setup standard error to go to standard output

Process p = new Process();
p.StartInfo = psi;
p.Start();

string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
// <<<<end snippet>>>>

Thanks!

--Edwin G. Castro
(e-mail address removed)


.
 
J

Jeffrey Tan[MSFT]

Hi Edwin,

I think you can use WorkingDirectory to set for the cmd.exe, it will keep
the space,something like this:

Process compiler = new Process();
compiler.StartInfo.FileName = "cmd.exe";
compiler.StartInfo.WorkingDirectory="D:\\course ware";
compiler.StartInfo.UseShellExecute = false;
compiler.StartInfo .CreateNoWindow =false;
compiler.StartInfo.RedirectStandardOutput = true;
compiler.Start();

Console.WriteLine(compiler.StandardOutput.ReadToEnd());
compiler.WaitForExit();

It works well on my machine.
Hope this helps,

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| Content-Class: urn:content-classes:message
| From: "Edwin G. Castro" <[email protected]>
| Sender: "Edwin G. Castro" <[email protected]>
| References: <[email protected]>
<[email protected]>
<[email protected]>
| Subject: Re: Redirection troubles II
| Date: Thu, 2 Oct 2003 09:33:13 -0700
| Lines: 161
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Thread-Index: AcOJAt/NL1vu8/f3R1CbNFeDiFBJpQ==
| Newsgroups: microsoft.public.dotnet.languages.csharp
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:188593
| NNTP-Posting-Host: TK2MSFTNGXA08 10.40.1.160
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Hi,
|
| Thanks for the recommendation! Unfortunately, that route
| doesn't work for me. The problem is with long pathnames
| (with embedded spaces). In my case the path to C:\test.exe
| would have spaces, C:\Some Directory\test.exe, forcing me
| to use quote charaters. Unfortunately, some of the
| parameters to the program I'm trying to run may have
| spaces in them also. The cmd.exe interpreter has a tough
| time dealing with more than one quoted string in it's
| arguments. The result is that cmd.exe reports an error
| saying that it can't find C:\Some. This is a limitation of
| cmd.exe.
|
| On the other hand, I did manage to fake redirecting
| standard error to standard output. I realized that I was
| only interested in the output one line at a time. So what
| I'm doing is checking if there is anything in standard
| output (using the Peek method) and reading one line if
| there was. Then I check standard error in the same way. I
| do this in an infinite loop until both streams are empty.
| Here is some code:
|
| // <<<<start snippet>>>>
| while (true)
| {
| if (process.StandardOutput.Peek() >=0)
| {
| string line = process.StandardOutput.ReadLine();
| // ...
| }
| else
| {
| if (process.StandardError.Peek() < 0) break;
| }
|
| if (process.StandardError.Peek() >= 0)
| {
| string line = process.StandardError.ReadLine();
| // ...
| }
| else
| {
| if (process.StandardOutput.Peek() < 0) break;
| }
| }
| // <<<<stop snippet>>>>
|
| If this seems unreasonable please let me know. It is quite
| possible that I haven't considered something.
|
| Thanks for the input!
|
| --Edwin G. Castro
| (e-mail address removed)
|
| PS - I'm from Puerto Rico; but now I live in Oregon. Is
| Havana University in Florida or Cuba...?
|
| >-----Original Message-----
| >Hi again,
| >
| > Here is a solution:
| >ProcessStartInfo psi = new ProcessStartInfo
| (@"c:\winnt\system32\cmd.exe",
| >@"/c c:\test.exe 2>&1");
| >
| >Cheers,
| >
| >--
| >Ignacio Machin, ( .NET/ C# MVP )
| >ignacio.machin AT dot.state.fl.us
| >Florida Department Of Transportation
| >
| >
| >"Ignacio Machin" <ignacio.machin AT dot.state.fl.us>
| wrote in message
| >| >> Hi Edwin,
| >>
| >> Did you try to instruct the program to redirect the
| standard error to
| >> standard output as you mention?
| >> I would play a little with this.
| >> Like this:
| >> ProcessStartInfo p = new ProcessStartInfo
| ("command", "\"ar g1\" \"arg2\"
| >> 2>&1");
| >>
| >>
| >> Hope this help.
| >>
| >> Pd:
| >> It may be a coincidence but I used to know a person
| with your name when I
| >> was studing at Havana univ.
| >> Cheers,
| >> --
| >> Ignacio Machin, ( MVP )
| >> ignacio.machin AT [dot.state.fl.us]
| >> Florida Department Of Transportation
| >>
| >>
| >>
| >> | >> > I want to start a process from a C# application. I
| also want to
| >> > redirect standard error to standard output so that I
| can read output
| >> > from both streams just like I could from a command
| line. In other
| >> > words, I want to emulate the following: command arg1
| arg2 > file.txt
| >> > 2>&1
| >> >
| >> > I know I can get StreamReaders for standard error and
| standard output
| >> > separately, but I want them interleaved like in the
| sample command
| >> > line above. I want to be able to read from standard
| output and get
| >> > both what was written on standard output and standard
| error.
| >> >
| >> > Any help would be appreciated!
| >> >
| >> > Following is an example of what I want to do:
| >> > // <<<<start snippet>>>>
| >> > // NOTE: the argument string needs to have quote
| characters because
| >> > some
| >> > // arguments have spaces in them.
| >> > ProcessStartInfo p = new ProcessStartInfo
| ("command", "\"ar g1\" \"ar
| >> > g2\"");
| >> > psi.CreateNoWindow = true;
| >> > psi.UseShellExecute = false;
| >> > psi.RedirectStandardOutput = true;
| >> >
| >> > // somehow setup standard error to go to standard
| output
| >> >
| >> > Process p = new Process();
| >> > p.StartInfo = psi;
| >> > p.Start();
| >> >
| >> > string output = p.StandardOutput.ReadToEnd();
| >> > p.WaitForExit();
| >> > // <<<<end snippet>>>>
| >> >
| >> > Thanks!
| >> >
| >> > --Edwin G. Castro
| >> > (e-mail address removed)
| >>
| >>
| >
| >
| >.
| >
|
 

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