Exploring the C# `System.IO` Namespace: File and Stream Manipulation
Master file and stream operations in C# with this guide to the `System.IO` namespace. Learn about key classes like `File`, `Directory`, `StreamReader`, `StreamWriter`, and more, enabling you to build robust and efficient file handling capabilities into your C# applications.
Exploring the C# `System.IO` Namespace
The `System.IO` namespace in C# provides a comprehensive set of classes, structures, delegates, and enumerations for working with files and streams. It's essential for any C# application that needs to read from or write to files or work with directories.
Classes in `System.IO`
Here's a summary of key classes in the `System.IO` namespace:
Class | Description |
---|---|
BinaryReader |
Reads binary data from a stream. |
BinaryWriter |
Writes binary data to a stream. |
BufferedStream |
Adds buffering to another stream for improved performance. |
Directory |
Provides static methods for working with directories. |
DirectoryInfo |
Provides instance methods for working with directories. |
DirectoryNotFoundException |
Exception thrown when a directory is not found. |
DriveInfo |
Provides information about a drive. |
DriveNotFoundException |
Exception thrown when a drive is not found. |
EndOfStreamException |
Exception thrown when trying to read past the end of a stream. |
ErrorEventArgs |
Provides data for the `FileSystemWatcher.Error` event. |
File |
Provides static methods for working with files. |
FileFormatException |
Exception thrown due to an invalid file format. |
FileInfo |
Provides properties and methods for working with files. |
FileLoadException |
Exception thrown when a file cannot be loaded. |
FileNotFoundException |
Exception thrown when a file is not found. |
FileStream |
Provides a stream for file operations. |
FileSystemEventArgs |
Provides data for file system events. |
FileSystemInfo |
Base class for `FileInfo` and `DirectoryInfo`. |
FileSystemWatcher |
Monitors a directory for changes. |
InternalBufferOverflowException |
Exception for buffer overflow errors. |
InvalidDataException |
Exception for invalid data in a stream. |
IOException |
Generic exception for I/O errors. |
MemoryStream |
Creates a stream in memory. |
Path |
Provides methods for manipulating file paths. |
PathTooLongException |
Exception thrown if a file path is too long. |
PipeException |
Exception related to named pipes. |
RenamedEventArgs |
Provides data for the `Renamed` event. |
StreamReader |
Reads characters from a byte stream. |
StringReader |
Reads characters from a string. |
StringWriter |
Writes characters to a string. |
TextReader |
Abstract base class for text readers. |
TextWriter |
Abstract base class for text writers. |
UnmanagedMemoryAccessor |
Provides access to unmanaged memory. |
UnmanagedMemoryStream |
Creates a stream over an unmanaged memory block. |
Structures in `System.IO`
Structure | Description |
---|---|
WaitForChangedResult |
Contains information about file system changes. |
Delegates in `System.IO`
Delegate | Description |
---|---|
ErrorEventHandler |
Handles errors from `FileSystemWatcher`. |
FileSystemEventHandler |
Handles file system changes (`Changed`, `Created`, `Deleted`). |
RenamedEventHandler |
Handles file rename events. |
Enumerations in `System.IO`
Enumeration | Description |
---|---|
DriveType |
Represents different types of drives. |
FileAccess |
Specifies file access modes (read, write). |
FileAttributes |
Represents file and directory attributes (e.g., read-only, hidden). |
FileMode |
Specifies how to open a file. |
FileOptions |
Provides advanced options for file streams. |
FileShare |
Controls how other processes can access the same file. |
HandleInheritability |
Determines if a handle can be inherited by child processes. |
NotifyFilters |
Specifies the types of file system changes to monitor. |
SearchOption |
Specifies search options (top-level directory only or all subdirectories). |
SeekOrigin |
Specifies the starting point for seeking within a stream. |
WatcherChangeTypes |
Indicates the types of changes detected by `FileSystemWatcher`. |