0Pricing
C# Academy · Lesson

System.IO (paths, streams)

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

System.IO (paths, streams) is a free C# Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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));
  }
}

Create and write

Use Directory.CreateDirectory (safe even if it exists) and File.WriteAllText for small files.

using System;
using System.IO;

public class Program
{
  public static void Main(string[] args)
  {
    string folder = "data";
    if (!Directory.Exists(folder))
    {
      Directory.CreateDirectory(folder); // create if missing
    }

    string path = Path.Combine(folder, "hello.txt");
    // Simple one-shot write
    File.WriteAllText(path, "Hello file!");
    Console.WriteLine("Wrote: " + path);
  }
}

Reading safely

Wrap readers in a using block. The file handle is released even if an exception occurs.

using System;
using System.IO;

public class Program
{
  public static void Main(string[] args)
  {
    string path = Path.Combine("data", "hello.txt");

    if (File.Exists(path))
    {
      using (StreamReader r = new StreamReader(path))
      {
        string content = r.ReadToEnd(); // read all
        Console.WriteLine("Content: " + content);
      } // Dispose closes the file
    }
    else
    {
      Console.WriteLine("Missing file: " + path);
    }
  }
}

Writing safely

StreamWriter writes text. Pass append=true to add lines without overwriting.

using System;
using System.IO;

public class Program
{
  public static void Main(string[] args)
  {
    string path = Path.Combine("data", "log.txt");

    // Append = true so we keep old content
    using (StreamWriter w = new StreamWriter(path, true))
    {
      w.WriteLine(DateTime.UtcNow.ToString("u") + " - started");
      w.WriteLine("Work done");
    } // closed here

    Console.WriteLine("Appended to log: " + path);
  }
}

Checks & tips

Tips:

  • Check File.Exists/Directory.Exists before using.
  • Catch IOException for I/O errors and report a friendly message.
  • Prefer using for all streams.

Stream safety rule

Quick check: What is the safest way to ensure a stream is closed after reading or writing in C# 6?

Recap

Recap: Build paths with Path, check existence, and always wrap readers/writers in a using block for safe cleanup.

Frequently asked questions

Is the “System.IO (paths, streams)” lesson free?

Yes — the full text of “System.IO (paths, streams)” is free to read here on the web, and the C# Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.

What will I learn in “System.IO (paths, streams)”?

Use Path helpers, check existence, and read/write text with StreamReader/StreamWriter using the using statement. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “System.IO (paths, streams)” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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