Like the members of the File class, all members in the Directory class are public
static methods. The Directory class provides static methods for creating and
moving through directories and subdirectories.
.NET also includes DirectoryInfo and FileInfo classes.These classes are very similar to
their name counterparts, Directory and File.They differ in that the DirectoryInfo
and FileInfo classes both have instance methods instead of static members.They are direct
descendents of the FileSystemInfo class.
The FileInfo and DirectoryInfo classes add functionality beyond method members of
the File and Directory classes. They both have a number of public properties and, of
course, both offer a public constructor. Neither class can be inherited. If you plan to make
reference to and use a file or directory several times, you will want to consider instantiating
objects of one or both of these classes and use its instance methods.
C# uses file streams to deal with stored data. Streams represent a chunk of data and provide a
way to work with a sequence of bytes. Several abstract classes, including Stream,
TextWriter, and TextReader, are defined for dealing with files.These classes are defined
in the System.IO namespace.The stream classes provide generic methods for dealing with
input/output, and these methods reduce the need for providing specific details about how the
operating system actually accesses data from particular devices.
The StreamWriter and StreamReader classes make it easy to read or write data to and
from text files in C#.The StreamWriter class has implementations for Write( ) and
WriteLine( ) methods similar to the Console class methods. StreamReader includes
implementations of Read( ) and ReadLine( ).The simplest constructor for these classes
includes a single argument for the name of the file.The name may include the full path indicating
where the file is located or you may use the relative path to the file. Relative paths are
relative to the starting location of the project.
Constructing an object of the StreamWriter class using it as the second argument
enables you to append values onto the end of a file. The following statement opens the file
named info.txt, stored in the project subdirectory, so that records wil