System.IO Namespace

Provides types that allow reading and writing files and data streams, and types that abstract file and directory access.

Summary of Members

Type Name Description
Class File Provides static methods for the creation, copying, deletion, moving, and opening of files, and aids in their creation, deletion, and manipulation.
Class Directory Provides static methods for the creation, moving, and enumeration of directories and subdirectories.
Class Path Exposes static members for creating, manipulating, and resolving file and directory path strings.
Class Stream Abstract base class of the .NET Framework implementation of streams.
Class FileStream Exposes a stream around a file, supporting both synchronous and asynchronous read and write operations.
Class StreamReader Implements a System.Text.Decoder that decodes a byte stream into a sequence of characters.
Class StreamWriter Implements a System.Text.Encoder that encodes a sequence of characters into a byte stream.
Class MemoryStream Creates a stream that is backed by an in-memory buffer.
Class BinaryReader Reads primitive data types from a stream in binary format.
Class BinaryWriter Writes primitive types to a stream in binary format.
Class DirectoryInfo Provides instance methods for the creation, moving, and enumeration of directories and subdirectories.
Class FileInfo Provides instance methods for the creation, moving, and enumeration of files, and aids in their creation, deletion, and manipulation.
Class IOException The exception that is thrown when an I/O error occurs.
Enum FileMode Specifies how the operating system should open a file.
Enum FileAccess Specifies the access to which an object is opened.

Usage Example

Here's a simple example of how to write to and read from a file using the System.IO namespace:

using System;
using System.IO;

public class FileIOExample
{
    public static void Main(string[] args)
    {
        string filePath = "example.txt";
        string textToWrite = "Hello, System.IO!";

        // Write to the file
        try
        {
            using (StreamWriter writer = new StreamWriter(filePath))
            {
                writer.WriteLine(textToWrite);
                Console.WriteLine($"Successfully wrote to {filePath}");
            }
        }
        catch (IOException ex)
        {
            Console.WriteLine($"An error occurred while writing: {ex.Message}");
        }

        // Read from the file
        try
        {
            using (StreamReader reader = new StreamReader(filePath))
            {
                string line = reader.ReadLine();
                Console.WriteLine($"Read from file: {line}");
            }
        }
        catch (IOException ex)
        {
            Console.WriteLine($"An error occurred while reading: {ex.Message}");
        }
        finally
        {
            // Clean up the file
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
                Console.WriteLine($"Deleted {filePath}");
            }
        }
    }
}