在c#窗口中将文本数据从文件重定向到标准

本文关键字:文件 重定向 标准 数据 文本 窗口 | 更新日期: 2023-09-27 18:09:15

如何将数据从一些文本文件重定向到c#程序的stdin?通过一些代码或命令提示符(在Windows中)?我试过在命令提示符中重定向它,像这样:input.txt> program.exe> output.txt(像在linux中),但这不起作用。我也试过这样做:

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);
        }
    }
}

但这也不起作用。它将崩溃并显示以下错误:

"类型为'System '的未处理异常。ArgumentException'发生在mscorlib.dll

附加信息:流不可写。"

我怎样才能做我想做的?

在c#窗口中将文本数据从文件重定向到标准

使用命令重定向操作符

 program.exe < input.txt > output.txt