James
James

Reputation: 131

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. output.txt, how could I do this in this context?

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

Upvotes: 1

Views: 1240

Answers (2)

BugFinder
BugFinder

Reputation: 17858

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

Upvotes: 3

Erick Petrucelli
Erick Petrucelli

Reputation: 14872

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

Upvotes: 1

Related Questions