Udostępnij za pośrednictwem


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.

For a list of common I/O tasks, see Common I/O Tasks.

Note

Visual Basic users may choose to use the methods and properties provided by the Microsoft.VisualBasic.FileIO.FileSystem class for file I/O.

Classes Used for File I/O

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.

DriveInfo provides instance methods for accessing information about a drive.

File provides static methods for the creation, copying, deletion, moving, and opening of files, and aids in the creation of a FileStream. The FileInfo class provides instance methods.

FileInfo provides instance methods for the creation, copying, deletion, moving, and opening of files, and aids in the creation of a FileStream. The File 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.

DeflateStream provides methods and properties for compressing and decompressing streams using the Deflate algorithm.

GZipStream provides methods and properties for compressing and decompressing streams. By default, this class uses the same algorithm as the DeflateStream class, but can be extended to use other compression formats.

SerialPort provides methods and properties for controlling a serial port file resource.

File, FileInfo, DriveInfo, 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

BinaryReader and BinaryWriter read and write encoded strings and primitive data types from and 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.

A NetworkStream represents a Stream over a network connection. Although NetworkStream derives from Stream, it is not part of the System.IO namespace, but is in the System.Net.Sockets namespace.

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.

Note

ACLs can be managed programmatically. For more information, see How to: Add or Remove Access Control List Entries and ACL Technology Overview.

Caution noteCaution

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 noteCaution

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.

See Also

Tasks

How to: Create a Directory Listing

How to: Read and Write to a Newly Created Data File

How to: Open and Append to a Log File

How to: Read Text from a File

How to: Write Text to a File

How to: Read Characters from a String

How to: Write Characters to a String

Concepts

Common I/O Tasks