开发者

Piping output to a text file in C#

开发者 https://www.devze.com 2023-03-11 03:25 出处:网络
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?

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

精彩评论

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