System.IO Namespace

System.IO Namespace

The System.IO namespace contains types that allow reading and writing to files and data streams, and types that provide basic file and directory support.

Quick Links

Classes

Explore the most commonly used classes.

Enumerations

Interfaces

Code Examples


// Write text to a file
using System;
using System.IO;

class Example
{
    static void Main()
    {
        string path = @"C:\temp\example.txt";
        File.WriteAllText(path, "Hello, World!");
        Console.WriteLine("File written successfully.");
    }
}

// Read all lines from a file
using System;
using System.IO;

class Example
{
    static void Main()
    {
        string path = @"C:\temp\example.txt";
        if (File.Exists(path))
        {
            string[] lines = File.ReadAllLines(path);
            foreach (var line in lines)
                Console.WriteLine(line);
        }
    }
}