tags:

views:

308

answers:

2

I'm trying to figure out how to read from the standard input stream of my own WPF application but I can't find a way to get the stream. The standard way to do it,

Console.ReadLine()

returns Null(Nothing) immediately. I'm assuming because it's not a console application and no console window is open. How can I read from standard input in a WPF application?

Some backgroud: I'm trying to read text being sent in by an external device (magnetic card reader) which sends its data to stdin of the currently focused application. I can set focus on a TextBox and then read the data from there if I have to but I would much rather read the data directly if possible to avoid a host of possible issues.

Edit: Even though Daniel Pratt solved my original problem it would still be nice to figure out how to read from stdin. If you can answer it I'll change the accepted answer.

+3  A: 

I was under the impression that devices of this sort usually function as a keyboard (this would make the part about the TextBox from the original question make sense). I think you just need to hook some keyboard events.

Daniel Pratt
A: 

Looking at the .NET framework code for reading/writing from the stdin/stdout streams, it appears as if there is nothing reliant on the console window in fact. The code appears to simply make use of various native Win32 calls, firstly to GetStdHandle to get the handle of the standard in/out stream, and the creates a *__ConsoleStream* class which is a managedd wrapper over the Win32 ReadFileNative and WriteFileNative functions. So really, I don't see any reason why Console.ReadLine shouldn't work... It's worth checking whether calls to read from stdin using Console succeed when you put the code directly in the Main function. I suppose the issue here could be something to do with the application type as recognised by Windows, but I don't see why non-console apps should be restricted from reading from stdin/writing to stdout. The other possible cause could be the thread from which you are making the calls. Anyway, have a go testing this and let me know the results. If you still don't have any success, I'll try to do some of my own testing as I'm curious as to the solution.

Noldorin