C# System.IO Namespace

Last Updated : 2 Jul 2026

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.

What is the System.IO Namespace in C#?

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.

Using the System.IO Namespace

To use the classes available in the System.IO namespace, include it at the beginning of your C# program.

Syntax

The following syntax is used to include the System.IO namespace in C#:

using System.IO;

In this syntax:

  • using imports a namespace into the program.
  • System.IO specifies the namespace that provides file and directory handling classes.

Common Classes in the System.IO Namespace

The System.IO namespace contains many classes for working with files, directories, streams, and memory.

ClassDescription
BinaryReaderIt is used to read primitive data types as binary values in a specific encoding.
BinaryWriterIt is used to write primitive types in binary to a stream.
BufferedStreamIt is used to add a buffering layer to read and write operations on another stream. It is a sealed class.
DirectoryIt is used to expose static methods for creating, moving and enumerating through directories and subdirectories. It is a sealed class.
DirectoryInfoIt is used to expose instance methods for creating, moving and enumerating through directories and subdirectories. It is a sealed class.
DirectoryNotFoundExceptionIt is used to handle exception related to the file or directory cannot be found.
DriveInfoIt is used to access the information on a drive.
DriveNotFoundExceptionIt is used to handle drive not found exception.
EndOfStreamExceptionIt is used to handle end of stream exception.
ErrorEventArgsIt provides data for the FileSystemWatcher.Error event.
FileThis class provides static methods for the creation, copying, deletion, moving and opening of a single file.
FileFormatExceptionIt is used to handle file format exception.
FileInfoIt is used to provide properties and instance methods for the creation, copying, deletion, moving and opening of files.
FileLoadExceptionIt is used to handle file load exception.
FileNotFoundExceptionIt is used to handle file load exception.
FileNotFoundExceptionIt is used to handle file not found exception.
FileStreamIt provides a Stream for a file, supporting both synchronous and asynchronous read and write operations.
FileSystemEventArgsIt provides data for the directory events.
FileSystemInfoIt provides the base class for both FileInfo and DirectoryInfo objects.
FileSystemWatcherIt listens to the file system change notifications and raises events when a directory or file in a directory, changes.
InternalBufferOverflowExceptionThis class is used to handle internal buffer overflow exception.
InvalidDataExceptionIt is used to handle invalid data exception.
IODescriptionAttributeIt sets the description visual designers can display when referencing an event, extender or property.
IOExceptionIt is an exception class that handles I/O errors.
MemoryStreamIt is used to create a stream whose backing store is memory.
PathIt performs operations on String instances that contain file or directory path information.
PathTooLongExceptionIt is an exception class and used to handle path too long exception.
PipeExceptionThis exception class is used to handle pipe related exception.
RenamedEventArgsIt is used to provide data for the Renamed event.
StreamIt is used to provide a generic view of a sequence of bytes. It is an abstract class.
StreamReaderIt is used to implement a TextReader that reads characters from a byte stream.
StringReaderIt is used to implement a TextReader that reads from a string.
StringWriterIt is used to implement a TextWriter for writing information to a string. The information is stored in an underlying StringBuilder.
TextReaderThis class is used to represent a reader that can read a sequential series of characters.
TextWriterThis class is used to represent a writer that can write a sequential series of characters.
UnmanagedMemoryAccessorIt is used to provide random access to unmanaged blocks of memory from managed code.
UnmanagedMemoryStreamIt is used to get access to unmanaged blocks of memory from managed code.

Structures in the System.IO Namespace

The System.IO namespace provides structures that store information related to file system operations.

StructureDescription
WaitForChangedResultIt contains information on the change that occurred.

Delegates in the System.IO Namespace

The System.IO namespace contains delegates that are used with file system events.

DelegatesDescription
ErrorEventHandlerIt represents the method that will handle the Error event of a FileSystemWatcher object.
FileSystemEventHandlerIt represents the method that will handle the Changed, Created or Deleted event of a FileSystemWatcher class.
RenamedEventHandlerIt represents the method that will handle the renamed event of a FileSystemWatcher class.

Enumerations in the System.IO Namespace

The System.IO namespace provides several enumerations that define file access modes, sharing options, stream positions, and other file-related settings.

EnumerationDescription
DriveTypeIt is used to define constants for drive types including CDRom, Fixed, Network etc.
FileAccessIt is used to define constants for read, write or read/write access to a file.
FileAttributesIt is used to provide attributes for files and directories.
FileModeIt is used to specify how the operating system should open a file.
FileOptionsIt is used to represents advanced options for creating a FileStream object.
FileShareIt is used to contain constants for controlling the kind of access other FileStream objects can have to the same file.
HandleInheritabilityIt specifies whether the underlying handle is inheritable by child processes.
NotifyFiltersIt is used to specify changes to watch for in a file or folder.
SearchOptionIt is used to specify whether to search the current directory or the current directory and all subdirectories.
SeekOriginIt is used to specify the position in a stream to use for seeking.
WatcherChangeTypesIt changes that might occur to a file or directory.

Examples of the System.IO Namespace

The following examples demonstrate how to use the System.IO namespace for common file operations.

Example 1: Creating and Writing to a Text File

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.

Example 2: Reading Data from a Text File

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#.


Next TopicC# Collections