console, stdin, pipe and readline.

  • Thread starter William Stacey [MVP]
  • Start date
W

William Stacey [MVP]

Trying to test a simple console app that reads from standard in if you pipe
input into it like:
"c:\ type config.txt | consoleinput.exe"

Seem to run into an issue. It seems like if you use a pipe, you can read
all the input lines fine until eof. However, if you need to then prompt for
input, the pipe is still hooked up so Read or ReadLine returns with -1 or
null after that point. So you can't prompt for "Hit a key to continue",
etc. I am guessing I need to somehow disconnect the pipe from stdin or
"refresh" the stdin stream so it again starts waiting for normal console
input. Have tried various things, but can't seem to again get normal Read
or Readline behavior. Any thoughts? TIA

== Test Method called from Main ===
static void ReadStdIn()
{
string line;
ArrayList al = new ArrayList(200);
while ( (line = Console.ReadLine()) != null )
{
al.Add(line);
}

string s = Console.ReadLine();
if ( s == null )
Console.WriteLine("Input is null.");
else
Console.WriteLine("Input is:" + s);

}
 
J

Jeffrey Tan[MSFT]

Hi William,

I have viewed your post, I will spend some time on it, I will reply to you
ASAP.

Thanks for your understanding.

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.
 
W

William Stacey [MVP]

Thanks Jeffrey. This may be normal behavior. However, things like "less"
seem to do just this after draining the pipe they prompt to hit any key
after a screenfull. They probably are doing some other trick like a new
process or something. Your help is appreciated. Cheers!
 
J

Jeffrey Tan[MSFT]

Hi William,

So far as I think, we should can use Console.OpenStandardInput method to
get the standard input and set back. Like this:

Stream st=Console.OpenStandardInput ();
StreamReader sr=new StreamReader(st);
Console.SetIn(sr);

But, I can not get this work as expected. Anyway, I will still spend some
time on it. Please wait a little more time.

Thanks for your understanding.

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.
 
J

Jeffrey Tan[MSFT]

Hi William,

Sorry for letting you wait for so long time.

I am current consulting internally, I have received some useful feedback
for this issue. I am still asked for further confirmation. Please wait a
little more time.

Thanks for your understanding.

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.
 
J

Jeffrey Tan[MSFT]

Hi William,

Sorry for letting you wait for so long time.

It seems that CreateFile Win32 API can be used to create the console input
stream through specify "CONIN$" as the lpFileName parameter. For more
information, please refer to "Consoles" section in CreateFile API in MSDN.

You may try to P/invoke it and then use FileStream class to wrap it.

=============================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

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.
 
J

Jeffrey Tan[MSFT]

Hi William,

Have you tried my suggestion? Do you still have any concern on this issue?

Please feel free to feedback. Thanks

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.
 
J

Jeffrey Tan[MSFT]

Hi William,

I am glad I can help you. You are welcome!!

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.
 

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