Basic File I/O
The abstract base class Stream supports reading and writing bytes. Stream integrates asynchronous support. Its default implementations define synchronous reads and writes in terms of their corresponding asynchronous methods, and vice versa.
All classes that represent streams inherit from the Stream class. The Stream class and its derived classes provide a generic view of data sources and repositories, isolating the programmer from the specific details of the operating system and underlying devices.
Streams involve these fundamental operations:
- Streams can be read from. Reading is the transfer of data from a stream into a data structure, such as an array of bytes.
- Streams can be written to. Writing is the transfer of data from a data source into a stream.
- Streams can support seeking. Seeking is the querying and modifying of the current position within a stream.
Depending on the underlying data source or repository, streams might support only some of these capabilities. For example, NetworkStreams do not support seeking. The CanRead, CanWrite, and CanSeek properties of Stream and its derived classes determine the operations that various streams support.
Classes Used for File I/O
FileObject provides a representation of a text file. Use the FileObject class to perform most typical text file operations such as reading, writing, appending, copying, deleting, moving, or renaming. You can also use FileObject to examine and, in some cases, set file attributes, encoding, and path information.
Directory provides static methods for creating, moving, and enumerating through directories and subdirectories. The DirectoryInfo class provides instance methods.
DirectoryInfo provides instance methods for creating, moving, and enumerating through directories and subdirectories. The Directory class provides static methods.
FileStream supports random access to files through its Seek method. FileStream opens files synchronously by default, but supports asynchronous operation as well. File contains static methods, and FileInfo contains instance methods.
FileSystemInfo is the abstract base class for FileInfo and DirectoryInfo.
Path provides methods and properties for processing directory strings in a cross-platform manner.
File, FileInfo, Path, Directory, and DirectoryInfo are sealed (in Microsoft Visual Basic, NotInheritable) classes. You can create new instances of these classes, but they cannot have derived classes.
Classes Used for Reading from and Writing to Streams
StreamReader reads characters from Streams, using Encoding to convert characters to and from bytes. StreamReader has a constructor that attempts to ascertain what the correct Encoding for a given Stream is, based on the presence of an Encoding-specific preamble, such as a byte order mark.
StreamWriter writes characters to Streams, using Encoding to convert characters to bytes.
StringReader reads characters from Strings. StringReader allows you to treat Strings with the same API, so your output can be either a Stream in any encoding or a String.
StringWriter writes characters to Strings. StringWriter allows you to treat Strings with the same API, so your output can be either a Stream in any encoding or a String.
TextReader is the abstract base class for StreamReader and StringReader. While the implementations of the abstract Stream class are designed for byte input and output, the implementations of TextReader are designed for Unicode character output.
TextWriter is the abstract base class for StreamWriter and StringWriter. While the implementations of the abstract Stream class are designed for byte input and output, the implementations of TextWriter are designed for Unicode character input.
Common I/O Stream Classes
A BufferedStream is a Stream that adds buffering to another Stream such as a NetworkStream. (FileStream already has buffering internally, and a MemoryStream does not need buffering.) A BufferedStream can be composed around some types of streams in order to improve read and write performance. A buffer is a block of bytes in memory used to cache data, thereby reducing the number of calls to the operating system.
A CryptoStream links data streams to cryptographic transformations. Although CryptoStream derives from Stream, it is not part of the System.IO namespace, but is in the System.Security.Cryptography namespace.
A MemoryStream is a nonbuffered stream whose encapsulated data is directly accessible in memory. This stream has no backing store and might be useful as a temporary buffer.
I/O and Security
When using the classes in the System.IO namespace, operating system security requirements such as access control lists (ACLs) must be satisfied for access to be allowed. This requirement is in addition to any FileIOPermission requirements.
CAUTION Default security policy for the Internet and an intranet does not allow access to files. Therefore, do not use the regular nonisolated storage I/O classes if you are writing code that will be downloaded over the Internet. Use Isolated Storage instead.
CAUTION When a file or network stream is opened, a security check is performed only when the stream is constructed. Therefore, be careful when handing these streams off to less-trusted code or application domains.
Creating a Directory Listing | Reading and Writing to a Newly Created Data File | Opening and Appending to a Log File | Reading Text from a File | Writing Text to a File | Reading Characters from a String | Writing Characters to a String