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

Redirection troubles

P: n/a
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
ec*****@hp.com
Nov 15 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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" <ec*****@hp.com> wrote in message
news:d9**************************@posting.google.c om...
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
ec*****@hp.com

Nov 15 '05 #2

P: n/a
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
news:eE*************@TK2MSFTNGP11.phx.gbl...
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" <ec*****@hp.com> wrote in message
news:d9**************************@posting.google.c om...
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
ec*****@hp.com


Nov 15 '05 #3

P: n/a
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
ec*****@hp.com

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 messagenews:eE*************@TK2MSFTNGP11.phx.gbl...
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" <ec*****@hp.com> wrote in message
news:d9**************************@posting.google.c om...
> 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
> ec*****@hp.com


.

Nov 15 '05 #4

P: n/a

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.ReadToEn d());
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" <ec*****@hp.com>
| Sender: "Edwin G. Castro" <ec*****@hp.com>
| References: <d9**************************@posting.google.com >
<eE*************@TK2MSFTNGP11.phx.gbl>
<uJ*************@TK2MSFTNGP10.phx.gbl>
| Subject: Re: Redirection troubles II
| Date: Thu, 2 Oct 2003 09:33:13 -0700
| Lines: 161
| Message-ID: <13****************************@phx.gbl>
| 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
| ec*****@hp.com
|
| 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
| >news:eE*************@TK2MSFTNGP11.phx.gbl...
| >> 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" <ec*****@hp.com> wrote in message
| >> news:d9**************************@posting.google.c om...
| >> > 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
| >> > ec*****@hp.com
| >>
| >>
| >
| >
| >.
| >
|

Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.