开发者

Piping output to a text file in C#

I have a method here, although I would like to pipe the output from it to a file e.g. outp开发者_StackOverflow社区ut.txt, how could I do this in this context?

  foreach (string tmpLine in File.ReadAllLines(@"c:\filename.txt"))
  {
    if (File.Exists(tmpLine))
    {
      //output
    }
  }


Normally command line wise, you do

mycommand > somefile.txt

or

mycommand | more

This works because output is written to std out.

You could try http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx


That's it:

var file = File.AppendText(@"c:\output.txt");

foreach (string tmpLine in File.ReadAllLines(@"c:\filename.txt"))
{
    if (File.Exists(tmpLine))
    {       
        file.WriteLine(tmpLine);
    }
}

file.Close();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜