Skip to content
dreamcode
dreamcode
Map
File I/O
Lesson 18 of 21
+15 XP on finish
C# ADVANCEDChapter 5 · C# Advanced

File I/O operations

The System.IO namespace contains classes for handling file operations. Standard static methods on the File class allow reading and writing in single operations.

Worked example
using System;
using System.IO;

class Program {
    static void Main() {
        string path = "sky.txt";
        File.WriteAllText(path, "starry");
        string content = File.ReadAllText(path);
        Console.WriteLine(content);
    }
}

How it reads

  • File.WriteAllText(...) writes a string to a file (creates or overwrites it)
  • File.ReadAllText(...) reads all text from a file into a single string
Cloud tip: Use static classes like File for quick, simple operations. For advanced streaming, use StreamReader or StreamWriter.

Check your understanding

0 / 2

Answer all 2 to complete this lesson and earn 15 XP.

  1. 1. Which namespace contains the File class?
  2. 2. Which method reads all contents of a file into a single string?
Answer every question to unlock the next lesson.