Sinmson
Sinmson

Reputation: 237

Difference File and Streamwriter class

What is the difference between using File from using System.IO & StreamWriter from using System.IO?

I am using File.WriteAllLines but in many internet-source-codes they are using:

using (StreamWriter writer = new StreamWriter("important.txt"))
{
   writer.Write("Word ");
}

Upvotes: 1

Views: 1082

Answers (1)

Chris
Chris

Reputation: 5514

File.WriteAllLines is simply a convenience method that internally uses a StreamWriter, which in turn uses a FileStream to write to the file.

The main difference is that while File.WriteAllLines only writes to files, a StreamWriter can write to any stream, for example a network stream.

Either way works, use what suits you the best.

Upvotes: 4

Related Questions