Redirecting text data from file to stdin c# windows

随声附和 提交于 2019-12-11 10:25:15

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!