2013-04-13 38 views
1

如何將數據從某些文本文件重定向到C#程序的stdin?通過一些代碼或命令提示符(在Windows中)?我已經嘗試在命令提示符中重定向它,如下所示:input.txt> program.exe> output.txt(如在Linux中),但這不起作用。也試過這樣:將文本數據從文件重定向到stdin c#windows

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

但是這也沒有工作。它擠壓以下錯誤:

"An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll

Additional information: Stream was not writable."

我該怎麼做我想要的?

回答