.NET Stream and File Operations
- Key Classes Related to File I/O
- Directory and File Operations
- Reading and Writing to Files and Streams
- Learning By Example: Adding Open and Save to FontPad
- Summary
IN THIS CHAPTER
Key Classes Related to File I/O
Directory and File Operations
Reading and Writing to Files and Streams
Learning By Example: Adding Open and Save to FontPad
One of the most important issues any programmer faces is how to go about storing and retrieving information on disk. This issue can be surprisingly complex. The .NET Base Class Library provides a number of classes that simplify the issue somewhat. The library is a substantial improvement over the file-related operations Visual Basic programmers had available to them previously.
This chapter focuses on the .NET namespaces related to directories, files, and synchronous and asynchronous reading from and writing to data streams. First, an overview is presented that details the key classes within the namespace. Then we get into files, streams, and data types. And finally, we write a file-monitoring application that demonstrates the use of these classes.
After reading this chapter, you should be able to do the following:
Manage directories and files including creating, deleting, and accessing their property information
Monitor the file system and respond to basic system events
Read and write files as streams of data both synchronously and asynchronously
Access file data at the binary level
Understand some of the basic design considerations for choosing a file I/O strategy
Key Classes Related to File I/O
Every application, image, database, message, and document is stored as some form of a file. Directories and files are the warehouses and boxes of our applications. All our applications spend time interacting with the file system at some level. And most of our applications require us to read, write, or manipulate files and directories.
The .NET System.IO namespace provides us with a rich set of tools to write effective file I/O code in a productive manner. This namespace encapsulates functionality related to both synchronous and asynchronous reading and writing to files and streams. Table 7.1 presents the key classes contained in the namespaces. These classes represent those primarily used by developers when implementing features based on directories, files, and streams.
TABLE 7.1 Key Classes of the System.IO Namespace
Class |
Description |
Managing Directories |
|
Directory |
The Directory class allows you to create, move, and enumerate directories and subdirectories. All of its methods are static and require a security check for each call. Consider the DirectoryInfo class if you plan to reuse the object and do not want the overhead of the security checks. |
DirectoryInfo |
The DirectoryInfo class is similar to the Directory class but contains only instance methods. All objects created using this class reference a specific directory. As a result, a security check is performed only when the instance is created. |
Path |
The Path class allows you to process directory strings in a crossplatform manner. |
Manipulating Files |
|
File |
The File class allows you to write code to create, copy, delete, move, and open files. All its methods are static and thus perform a security check on every call. If you plan to reuse the File instance on a single file, use the instance class, FileInfo. |
FileInfo |
The FileInfo class is similar to the File, but like DirectoryInfo, provides instance methods. Reading and Writing to Files and Streams |
FileStream |
The FileStream class allows you to create a stream instance based on a file. |
StreamReader |
The StreamReader class implements a TextReader object that reads characters from a byte stream in a particular encoding. |
StreamWriter |
The StreamWriter class implements a TextWriter object for writing characters to a stream in a particular encoding. |
StringReader |
The StringReader class implements a TextReader object that reads from a string. |
StringWriter |
The StringWriter class implements a TextWriter object that writes information to a string. The information is stored in an underlying StringBuilder class (from the System.Text namespace). |
BinaryReader |
The BinaryReader class allows you to access file data at the binary level. |
BinaryWriter |
The BinaryWriter class allows you to write binary data out to files. |
Monitoring File System Activity |
|
FileSystemWatcher |
The FileSystemWatcher class allows you to listen to the file system change notifications and intercept events when a directory or file changes. |