问题
How can I redirect data from some text file to the C# program's stdin? Via some code or command prompt (in Windows)? I've tried redirecting it in command prompt like this: input.txt > program.exe > output.txt (like in linux), but that's not working. Also tried it this way:
string path = @"D:\input.txt";
// Open the file to read from.
using (StreamReader sr = File.OpenText(path))
{
string s = "";
using (StreamWriter stdinput = new StreamWriter(Console.OpenStandardInput()))
{
while ((s = sr.ReadLine()) != null)
{
stdinput.WriteLine(s);
}
}
}
But this didn't work either. It crushes with the following error:
"An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll
Additional information: Stream was not writable."
How can I do what I want?
回答1:
Using command redirection operators
program.exe < input.txt > output.txt
来源:https://stackoverflow.com/questions/15991361/redirecting-text-data-from-file-to-stdin-c-sharp-windows