Getting StandardIn has not been redirected when starting new Process

A

Andrew Falanga

Hi,

I'm struggling to understand how to properly use the
System.Diagnostics.Process class correctly. Here's what I'm doing:

Process myProcess = new Process();

myProcess.StartInfo.FileName = @"c:\windows
\system32\cmd.exe";
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.RedirectStandardOutput = true;
myProcess.StartInfo.RedirectStandardInput = true;
myProcess.StandardInput.AutoFlush = true;
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();

myProcess.StandardInput.WriteLine("C:");
myProcess.StandardInput.WriteLine("cd C:\\Program Files\
\Microsoft Visual Studio 9.0\\VC");
myProcess.StandardInput.WriteLine("vcvarsall.bat");
myProcess.StandardInput.WriteLine("D:");


I can't execute any of the commands without getting the exception,
"StandardIn has not been redirected." Yet, I have
myProcess.StartInfo.RedirectStandardInput set to true.

I've done some searches on the net and I've not seen much different
than what I'm doing. Except, and most notably, in all cases I see the
instantiation of a System.Diagnostics.ProcessStartInfo object and then
a call to Process as in:

System.Diagnostics.ProcessStartInfo procStartInfo = new
System.Diagnostics.ProcessStartInfo();
// set the attributes

System.Diagnostics.Process myProc = new System.Diagnostics.Process
(procStartInfo);

I don't quite understand how that would make it different, but I've
not seen any example on the net that does it as I'm currently doing
it.

Also, where are StandardInput and StandardOutput being redirected?

Andy
 
P

Peter Duniho

[...]
I've done some searches on the net and I've not seen much different
than what I'm doing. Except, and most notably, in all cases I see the
instantiation of a System.Diagnostics.ProcessStartInfo object and then
a call to Process as in:

System.Diagnostics.ProcessStartInfo procStartInfo = new
System.Diagnostics.ProcessStartInfo();
// set the attributes

System.Diagnostics.Process myProc = new System.Diagnostics.Process
(procStartInfo);

I don't quite understand how that would make it different, but I've
not seen any example on the net that does it as I'm currently doing
it.

Have you tried doing it the other way? That's how I've always done it.
The docs clearly state that the approach you're taking is supposed to
work. But I admit, I've never actually tried it that way. I don't have
time at the moment to try to reproduce your issue, but it surprises me
that you are having trouble. For the time being, you might as well try
doing it the other way though.
Also, where are StandardInput and StandardOutput being redirected?

To and from the Stream instances created for the purpose, and which are
wrapped by the StreamWriter and StreamReader exposed by the StandardInput
and StandardOutput properties of the Process instance in question.

Pete
 
A

Andrew Falanga

[...]
I've done some searches on the net and I've not seen much different
than what I'm doing.  Except, and most notably, in all cases I see the
instantiation of a System.Diagnostics.ProcessStartInfo object and then
a call to Process as in:
System.Diagnostics.ProcessStartInfo procStartInfo = new
System.Diagnostics.ProcessStartInfo();
// set the attributes
System.Diagnostics.Process myProc = new System.Diagnostics.Process
(procStartInfo);
I don't quite understand how that would make it different, but I've
not seen any example on the net that does it as I'm currently doing
it.

Have you tried doing it the other way?  That's how I've always done it. 
The docs clearly state that the approach you're taking is supposed to  
work.  But I admit, I've never actually tried it that way.  I don't have  
time at the moment to try to reproduce your issue, but it surprises me  
that you are having trouble.  For the time being, you might as well try 
doing it the other way though.
Also, where are StandardInput and StandardOutput being redirected?

To and from the Stream instances created for the purpose, and which are  
wrapped by the StreamWriter and StreamReader exposed by the StandardInput 
and StandardOutput properties of the Process instance in question.

Pete

I just tried it the "other" way and I don't really understand why, but
I didn't get that exception. In fact, everything appears to have
worked. I'm a bit skeptical because it went a little too fast.

If anyone here can explain why starting this process in the fashion I
did above (the second "way"), please enlighten me. I don't understand
why it worked when the other didn't.

Thanks,
Andy
 
P

Peter Duniho

I just tried it the "other" way and I don't really understand why, but
I didn't get that exception. In fact, everything appears to have
worked. I'm a bit skeptical because it went a little too fast.

If anyone here can explain why starting this process in the fashion I
did above (the second "way"), please enlighten me. I don't understand
why it worked when the other didn't.

Me either. The docs say it should work, and on my computer it does (see
below for a concise-but-complete code example that works fine on my
computer...the output redirection wasn't strictly necessary, but it helps
keep the output between the two processes more clear and in order :) ).

Post a concise-but-complete code example that reliably demonstrates the
problem. Also, state specifically what OS and .NET versions you are
using. I am testing on Windows 7, .NET 3.5.

Pete


using System;
using System.Diagnostics;

namespace TestRedirectStdErr
{
class Program
{
static void Main(string[] args)
{
try
{
Process process = new Process();

process.StartInfo.FileName = "cmd.exe";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.Start();

process.StandardInput.WriteLine("exit");

string strOutput;
while ((strOutput = process.StandardOutput.ReadLine()) !=
null)
{
Console.WriteLine("\"" + strOutput + "\"");
}

Console.WriteLine("process completed successfully");
}
catch (Exception exc)
{
Console.WriteLine("Exception: \"" + exc.Message + "\"");
Console.WriteLine("Stack trace:");
Console.WriteLine(exc.StackTrace.ToString());
}

Console.ReadLine();
}
}
}
 
A

Andrew Falanga

I just tried it the "other" way and I don't really understand why, but
I didn't get that exception.  In fact, everything appears to have
worked.  I'm a bit skeptical because it went a little too fast.
If anyone here can explain why starting this process in the fashion I
did above (the second "way"), please enlighten me.  I don't understand
why it worked when the other didn't.

Me either.  The docs say it should work, and on my computer it does (see  
below for a concise-but-complete code example that works fine on my  
computer...the output redirection wasn't strictly necessary, but it helps 
keep the output between the two processes more clear and in order :) ).

Post a concise-but-complete code example that reliably demonstrates the  
problem.  Also, state specifically what OS and .NET versions you are  
using.  I am testing on Windows 7, .NET 3.5.

Pete

using System;
using System.Diagnostics;

namespace TestRedirectStdErr
{
     class Program
     {
         static void Main(string[] args)
         {
             try
             {
                 Process process = new Process();

                 process.StartInfo.FileName = "cmd.exe";
                 process.StartInfo.UseShellExecute = false;
                 process.StartInfo.RedirectStandardInput = true;
                 process.StartInfo.RedirectStandardOutput = true;
                 process.Start();

                 process.StandardInput.WriteLine("exit");

                 string strOutput;
                 while ((strOutput = process.StandardOutput.ReadLine()) !=  
null)
                 {
                     Console.WriteLine("\"" + strOutput + "\"");
                 }

                 Console.WriteLine("process completed successfully");
             }
             catch (Exception exc)
             {
                 Console.WriteLine("Exception: \"" + exc.Message + "\"");
                 Console.WriteLine("Stack trace:");
                 Console.WriteLine(exc.StackTrace.ToString());
             }

             Console.ReadLine();
         }
     }

}

Actually, what I did post was a copy/paste from my code. I'm using
Windows XP and .NET 3.5.

Thanks for the help.

Andy
 
P

Peter Duniho

Actually, what I did post was a copy/paste from my code.

But incomplete. No one can take the code you posted and just compile and
run it.

If you're unwilling to bother to post a complete code example, it's not
even possible to verify a problem, never mind suggest a solution. In that
case, you may prefer to compile and execute the concise-but-complete code
example I posted, to see if it fails on your computer or not.

I've already stated it works fine on mine, so either it will work fine on
yours too (in which case you've got a bug in your own code, in the part
you didn't bother to post), or it will fail on yours just as your own code
does (in which case you've shown that there's something wrong with your
configuration, either because of a OS or framework version issue, or
simply some sort of corruption in the installed software).

Pete
 

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