0Pricing
C# Academy · Lesson

System.IO (paths, streams)

Use Path helpers, check existence, and read/write text with StreamReader/StreamWriter using the using statement.

Intro to System.IO

Goal: Read and write files safely.

  • Build paths with Path
  • Check with File/Directory
  • Use StreamReader/StreamWriter in a using block

Path basics

Use Path.Combine and friends to avoid manual separators and to inspect parts of a path.

using System;
using System.IO;

public class Program
{
  public static void Main(string[] args)
  {
    // Build a safe path (handles separators)
    string folder = "data";
    string file = "notes.txt";
    string path = Path.Combine(folder, file);

    Console.WriteLine("File name: " + Path.GetFileName(path));
    Console.WriteLine("Directory: " + Path.GetDirectoryName(path));
    Console.WriteLine("Full path: " + Path.GetFullPath(path));
  }
}

All lessons in this course

  1. System.IO (paths, streams)
  2. JSON with System.Text.Json (opt-ins, converters)
  3. Simple CSV parsing patterns
← Back to C# Academy