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();
精彩评论