Developers often have to access directories and files in order to gather information and make necessary changes to the file system, as well as to modify the content of individual files. In this article, we will discuss the System.IO namespace, which provides necessary classes for working with directories and files. We will use a simple C# application as an example. (Note: The examples provided require the System.IO namespace.)

System.IO namespace

The System.IO namespace offers classes, methods, and properties that are necessary for working with directories and files. The summary of major classes is provided below. For a full description of the namespace and classes, see Microsoft's Web site.

  • BinaryReader and BinaryWriter: Allows reading and writing of primitive data types.
  • Directory and DirectoryInfo: Allows creating, moving, and enumerating through directories and subdirectories.
  • File and FileInfo: Allows creation, copying, deletion, moving, and opening of files.
  • FileStream: Allows accessing files in random fashion.
  • FileSystemInfo: Provides the base class for FileInfo and DirectoryInfo objects.
  • FileSystemWatcher: Raises events when a directory or file in a directory changes.
  • StreamReader, StreamWriter, StringReader, StringWriter: Allows reading and writing of textual information.
  • TextReader and TextWriter: Allows reading and writing of sequential series of characters.

Now I'll provide you with more in-depth details about five of these classes.

Get developer tips in your inbox
TechRepublic's free Software/Web Development NetNote newsletter highlights key topics, including XML, Java, .NET, SQL Server, C#, and Web services.
Automatically sign up today!

FileSystemInfo class

This class serves as a basis for both DirectoryInfo and FileInfo objects. You can't create an instance of the FileSystemInfo class because it's an abstract class, but you can use its methods and properties. Here's a list of properties and methods, along with the purpose of each.

  • Attributes: Get the attributes that are associated with the file.
  • CreationTime: Get the date/time when the file was created.
  • Exists: Check if the file exists.
  • Extension: Get the file extension.
  • LastAccessTime: Get the last accessed date/time of the file.
  • FullName: Get the full path of the file.
  • LastWriteTime: Get the last written activity date/time of the file.
  • Name: Get the name of the file.
  • Delete: Delete a file.

DirectoryInfo class

This class allows creating, copying, moving, deleting, renaming, and listing contents of the directories. The following examples utilize the DirectoryInfo class. Listing A contains the code to get the FullNameproperty of the directory. To create a subdirectory, you can use the code example in Listing B.

FileInfo class

This class allows creating, copying, moving, deleting, opening files. The following examples utilize the FileInfo class. Listing C contains the necessary code to get the Creation date time property of the file. To get the Length of the file, use the code in Listing D. To delete the file, you can use the code example in Listing E.

StreamWriter and StreamReader classes

View Listing F to look at the code example for writing text to a file. Check out Listing G to read the contents of the file line-by-line. We can also read the full content of the file, as in Listing H.

Using the Open() method

The Open() method allows reading and writing to the file as in Listing I. The following table lists the FileMode Enumeration values, along with their descriptions:

  • Append: Use to open the file and add data to it. This should be used with the FileAccess Write value.
  • Create: Use to create a new file and to overwrite an existing file with the same name.
  • CreateNew: Use to create a new file. However, if a file with the same name already exists, an IOException is thrown.
  • Open: Use to open a file.
  • OpenOrCreate: Use to open or create a new file. If the file with a given name exists, it will be opened; otherwise, it will be created.
  • Truncate: Use to truncate an existing file.

The following table lists the FileAccess Enumeration values:

  • Read: Use to retrieve the data from the file.
  • ReadWrite: Use to add or retrieve the data.
  • Write: Use to add the data to the file.

In this example, we demonstrated ways to access directories and files with C# by utilizing the System.IO namespace.

Irina Medvinskaya has been involved in technology since 1996. She has an MBA from Pace University and works as a Project Manager at Citigroup.