473,396 Members | 1,749 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Redirection troubles

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
4 2099
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
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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Albert Ahtenberg | last post by:
Hello, I don't know if it is only me but I was sure that header("Location:url") redirects the browser instantly to URL, or at least stops the execution of the code. But appearantely it continues...
1
by: Rob | last post by:
Im having some troubles. When a user selects a topic from a drop down I want it so that when they click submit it brings them to a corresponding html page so if they select plumbing then hit...
52
by: Gerard M Foley | last post by:
Can one write a webpage which is not displayed but which simply redirects the user to another page without any action by the user? Sorry if this is simple, but I am sometimes simple myself. ...
15
by: Taki Jeden | last post by:
Hello everybody Does anybody know why w3c validator can not get pages that use 404 htaccess redirection? I set up two web sites so that clients request non-existent urls, but htaccess redirects...
2
by: Nadav | last post by:
Hi, Introduction: *************** I am trying to redirect stdout to a RichEdit control, this is done by initiating a StringWriter, associated it with a StringBuilder and setting the...
8
by: Luciano A. Ferrer | last post by:
Hi! I was following the http://www.seomoz.org/articles/301-redirects.php article, trying to do that with one of my test sites I added this to the .htaccess file: RewriteEngine On RewriteCond...
13
by: souissipro | last post by:
Hi, I have written a C program that does some of the functionalities mentionned in my previous topic posted some days ago. This shell should: 1- execute input commands from standard input,...
1
by: comp.lang.php | last post by:
require_once("/users/ppowell/web/php_global_vars.php"); if ($_GET) { // INITIALIZE VARS $fileID = @fopen("$userPath/xml/redirect.xml", 'r'); $stuff = @fread($fileID,...
13
by: Massimo Fabbri | last post by:
Maybe it's a little OT, but I'll give it try anyway.... I was asked to maintain and further develop an already existing small company's web site. I know the golden rule of "eternal" URIs, but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.