When you are first learning the .NET Framework—and even for some time after—the proper way to read to, write from, or otherwise interact with files can be unclear because the framework provides so many different ways of attacking this problem. How should you determine which approach fits your scenario?
Use file streams to perform various file functions. There are five basic types of built-in file stream manipulation classes that you can use in order to read and/or write to the file stream:
For the most fine-grained control, use
for file manipulation since it provides the most low-level access to
the file, and, therefore, the most complex actions become available.
Some of these actions are reading and writing files in both
synchronous and asynchronous fashions, methods to lock and unlock
part or all of a file, seek a particular position in a file, or even
read the file as a stream of either characters or bytes.
This type is derived from the abstract base class
class is designed for reading character or string input from a file.
This class contains methods to read single characters, blocks of
characters, lines of characters, or even the whole file into a single
This class derives from the
TextWriter class. It is designed for writing character or string output to a file. This class contains methods ...