The System.IO namespace provides classes and types for working with files, directories, and data streams in C#. It enables applications to create, read, write, copy, move, and manage files and folders efficiently.
In this chapter, you will learn what the System.IO namespace is, its commonly used classes, structures, delegates, enumerations, and practical examples of using the namespace.
The System.IO namespace is a built-in namespace in C# that contains classes, structures, delegates, and enumerations for performing input/output (I/O) operations. It provides functionality to work with files, directories, streams, memory, and path information.
Developers commonly use the System.IO namespace to create, read, write, copy, move, and delete files and directories.
To use the classes available in the System.IO namespace, include it at the beginning of your C# program.
The following syntax is used to include the System.IO namespace in C#:
using System.IO;
In this syntax:
The System.IO namespace contains many classes for working with files, directories, streams, and memory.
| Class | Description |
|---|---|
| BinaryReader | It is used to read primitive data types as binary values in a specific encoding. |
| BinaryWriter | It is used to write primitive types in binary to a stream. |
| BufferedStream | It is used to add a buffering layer to read and write operations on another stream. It is a sealed class. |
| Directory | It is used to expose static methods for creating, moving and enumerating through directories and subdirectories. It is a sealed class. |
| DirectoryInfo | It is used to expose instance methods for creating, moving and enumerating through directories and subdirectories. It is a sealed class. |
| DirectoryNotFoundException | It is used to handle exception related to the file or directory cannot be found. |
| DriveInfo | It is used to access the information on a drive. |
| DriveNotFoundException | It is used to handle drive not found exception. |
| EndOfStreamException | It is used to handle end of stream exception. |
| ErrorEventArgs | It provides data for the FileSystemWatcher.Error event. |
| File | This class provides static methods for the creation, copying, deletion, moving and opening of a single file. |
| FileFormatException | It is used to handle file format exception. |
| FileInfo | It is used to provide properties and instance methods for the creation, copying, deletion, moving and opening of files. |
| FileLoadException | It is used to handle file load exception. |
| FileNotFoundException | It is used to handle file load exception. |
| FileNotFoundException | It is used to handle file not found exception. |
| FileStream | It provides a Stream for a file, supporting both synchronous and asynchronous read and write operations. |
| FileSystemEventArgs | It provides data for the directory events. |
| FileSystemInfo | It provides the base class for both FileInfo and DirectoryInfo objects. |
| FileSystemWatcher | It listens to the file system change notifications and raises events when a directory or file in a directory, changes. |
| InternalBufferOverflowException | This class is used to handle internal buffer overflow exception. |
| InvalidDataException | It is used to handle invalid data exception. |
| IODescriptionAttribute | It sets the description visual designers can display when referencing an event, extender or property. |
| IOException | It is an exception class that handles I/O errors. |
| MemoryStream | It is used to create a stream whose backing store is memory. |
| Path | It performs operations on String instances that contain file or directory path information. |
| PathTooLongException | It is an exception class and used to handle path too long exception. |
| PipeException | This exception class is used to handle pipe related exception. |
| RenamedEventArgs | It is used to provide data for the Renamed event. |
| Stream | It is used to provide a generic view of a sequence of bytes. It is an abstract class. |
| StreamReader | It is used to implement a TextReader that reads characters from a byte stream. |
| StringReader | It is used to implement a TextReader that reads from a string. |
| StringWriter | It is used to implement a TextWriter for writing information to a string. The information is stored in an underlying StringBuilder. |
| TextReader | This class is used to represent a reader that can read a sequential series of characters. |
| TextWriter | This class is used to represent a writer that can write a sequential series of characters. |
| UnmanagedMemoryAccessor | It is used to provide random access to unmanaged blocks of memory from managed code. |
| UnmanagedMemoryStream | It is used to get access to unmanaged blocks of memory from managed code. |
The System.IO namespace provides structures that store information related to file system operations.
| Structure | Description |
|---|---|
| WaitForChangedResult | It contains information on the change that occurred. |
The System.IO namespace contains delegates that are used with file system events.
| Delegates | Description |
|---|---|
| ErrorEventHandler | It represents the method that will handle the Error event of a FileSystemWatcher object. |
| FileSystemEventHandler | It represents the method that will handle the Changed, Created or Deleted event of a FileSystemWatcher class. |
| RenamedEventHandler | It represents the method that will handle the renamed event of a FileSystemWatcher class. |
The System.IO namespace provides several enumerations that define file access modes, sharing options, stream positions, and other file-related settings.
| Enumeration | Description |
|---|---|
| DriveType | It is used to define constants for drive types including CDRom, Fixed, Network etc. |
| FileAccess | It is used to define constants for read, write or read/write access to a file. |
| FileAttributes | It is used to provide attributes for files and directories. |
| FileMode | It is used to specify how the operating system should open a file. |
| FileOptions | It is used to represents advanced options for creating a FileStream object. |
| FileShare | It is used to contain constants for controlling the kind of access other FileStream objects can have to the same file. |
| HandleInheritability | It specifies whether the underlying handle is inheritable by child processes. |
| NotifyFilters | It is used to specify changes to watch for in a file or folder. |
| SearchOption | It is used to specify whether to search the current directory or the current directory and all subdirectories. |
| SeekOrigin | It is used to specify the position in a stream to use for seeking. |
| WatcherChangeTypes | It changes that might occur to a file or directory. |
The following examples demonstrate how to use the System.IO namespace for common file operations.
This example shows how to create a text file and write data to it by using the File class from the System.IO namespace.
using System;
using System.IO;
class Program
{
static void Main()
{
string path = "sample.txt";
File.WriteAllText(path, "Welcome to C# System.IO Namespace.");
Console.WriteLine("File created and data written successfully.");
}
}
Output:
File created and data written successfully.
Explanation
In this example, we use the File.WriteAllText() method to create a text file and write a string to it. If the file already exists, its contents are replaced with the new text.
This example demonstrates how to read the contents of a text file by using the File.ReadAllText() method.
using System;
using System.IO;
class Program
{
static void Main()
{
string path = "sample.txt";
File.WriteAllText(path, "Welcome to C# System.IO Namespace.");
Console.WriteLine("File created and data written successfully.");
}
}
Output:
File Content: Welcome to C# System.IO Namespace.
Explanation
In this example, we first check whether the file exists by using the File.Exists() method. If the file is available, the File.ReadAllText() method reads its entire contents and displays them on the console. This is one of the simplest ways to read text from a file in C#.
We request you to subscribe our newsletter for upcoming updates.