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. This is 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, and your progress syncs across the web and the CoddyKit app. The C# Academy course includes 3 lessons in total.
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
Recap
Recap: Build paths with Path, check existence, and always wrap readers/writers in a using block for safe cleanup.
Learn C# with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 93
- Lessons
- 346
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. 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. The C# Academy course includes 3 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 3.
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
- System.IO (paths, streams)
- JSON with System.Text.Json (opt-ins, converters)
- Simple CSV parsing patterns