代码之家  ›  专栏  ›  技术社区  ›  Seatless

将文本数据从文件重定向到stdin c#windows

  •  1
  • Seatless  · 技术社区  · 11 年前

    如何将数据从某些文本文件重定向到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

    附加信息:流不可写。"

    我该怎么做我想做的事?

    1 回复  |  直到 11 年前
        1
  •  10
  •   Maifee Ul Asad    4 年前

    Using command redirection operators

     program.exe < input.txt > output.txt