Input/Output (IO) in .NET Framework

This section covers the fundamental classes and concepts for performing input and output operations in .NET Framework applications. IO operations allow your applications to interact with external resources like files, streams, and the console.

Core Concepts

The .NET Framework provides a rich set of classes for handling IO, primarily found within the System.IO namespace. Key concepts include:

Working with Streams

Streams are fundamental. They are abstract and can represent data from various sources, such as files, network connections, or in-memory buffers.

Common Stream Types:

All stream classes inherit from the abstract Stream class, which provides methods like Read(), Write(), Seek(), and Flush().

Example: Reading from a File

using System;
using System.IO;

public class FileReader
{
    public static void Main(string[] args)
    {
        string filePath = "example.txt";
        try
        {
            // Open a StreamReader to read text from a file
            using (StreamReader sr = new StreamReader(filePath))
            {
                string line;
                // Read line by line until the end of the file
                while ((line = sr.ReadLine()) != null)
                {
                    Console.WriteLine(line);
                }
            }
        }
        catch (FileNotFoundException)
        {
            Console.WriteLine($"Error: The file '{filePath}' was not found.");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"An error occurred: {ex.Message}");
        }
    }
}

File Operations

The File class provides static methods for common file operations, such as creating, copying, deleting, and moving files.

Common File Class Methods:

Directory Operations

Similarly, the Directory class provides static methods for managing directories.

Common Directory Class Methods:

Working with Text (Readers and Writers)

For character-based IO, you'll often use classes like StreamReader and StreamWriter, which wrap byte-based streams to handle encoding and character manipulation.

StreamWriter Example: Writing to a File

using System;
using System.IO;

public class FileWriter
{
    public static void Main(string[] args)
    {
        string filePath = "output.txt";
        try
        {
            // Open a StreamWriter to write text to a file
            using (StreamWriter sw = new StreamWriter(filePath))
            {
                sw.WriteLine("This is the first line.");
                sw.WriteLine("This is the second line.");
                sw.Write("This is a single line without a newline.");
                sw.WriteLine(" And this continues on the same line.");
            }
            Console.WriteLine($"Successfully wrote to '{filePath}'");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"An error occurred: {ex.Message}");
        }
    }
}

Best Practices

Tip: Path Manipulation

The Path class provides utility methods for working with file and directory paths in a platform-independent manner (e.g., Path.Combine(), Path.GetFileName()).

Important: File Locking

When a file is opened by one process, it might be locked, preventing other processes from accessing it. Be mindful of file locking behavior and handle potential IOExceptions.

Further Reading