I’d like my app to read from files specified by command-line argument or from standard in, so the user can use it myprogram.exe data.txt or otherprogram.exe | myprogram.exe. How can I do this in C#?
In Python, I’d write
import fileinput
for line in fileinput.input():
process(line)
This iterates over the lines of all files listed in sys.argv[1:], defaulting to sys.stdin if the list is empty. If a filename is ‘-‘, it is also replaced by sys.stdin.
Perl’s <> and Ruby’s ARGF are similarly useful .
stdinis exposed to you as aTextReaderthroughConsole.In. Just declare aTextReadervariable for your input that either usesConsole.Inor the file of your choosing and use that for all your input operations.Otherwise if refactoring to use this new variable would be too expensive, you could always redirect
Console.Into your file usingConsole.SetIn().