将文本数据从文件重定向到Stdin C#Windows [英] Redirecting text data from file to stdin c# windows
问题描述
如何将数据从某些文本文件重定向到C#程序的stdin?通过某些代码或命令提示符(在Windows中)?我曾尝试在命令提示符下将其重定向为:input.txt> program.exe> output.txt(例如在Linux中),但这不起作用.也尝试过这种方式:
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:
发生了类型为'System.ArgumentException'的未处理异常 mscorlib.dll
"An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll
其他信息:流不可写."
Additional information: Stream was not writable."
我该怎么办?
推荐答案
program.exe < input.txt > output.txt
这篇关于将文本数据从文件重定向到Stdin C#Windows的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!