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 ");
}
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.