# Files and Exceptions

Programs read and write files, and things go wrong. You will work with files and paths, read and write text files, handle exceptions, and log errors so you can find them later.

Updated: November 3, 2026

## [Working with Files in C#](https://www.ask-codey.com/courses/csharp-full-stack-bootcamp/topics/working-with-files-in-csharp)

1. [Working with Files in C#: an overview](https://www.ask-codey.com/lessons/working-with-files-in-csharp-an-overview) (Apprentice): A program finds a file by its path. An absolute path starts at the drive; a relative path starts at the program's current folder, which in Visual Studio is the build folder, so data files are copied there.
2. [File, FileInfo and Path](https://www.ask-codey.com/lessons/working-with-files-in-csharp-file-fileinfo-and-path) (Apprentice): File and Directory check for, create, list, copy and delete files and folders. FileInfo describes one file. Path builds paths with Path.Combine and takes them apart, without touching the disk.

## [Exceptions in C#](https://www.ask-codey.com/courses/csharp-full-stack-bootcamp/topics/exceptions-in-csharp)

1. [Exceptions in C#](https://www.ask-codey.com/lessons/exceptions-in-csharp) (Apprentice): When something goes wrong at run time, C# throws an exception, and the program crashes unless code catches it. Prevent what you can check for; catch what you can't, most specific type first; and throw your own when a method can't do what it's asked.

## [Reading a File in C#](https://www.ask-codey.com/courses/csharp-full-stack-bootcamp/topics/reading-a-file-in-csharp)

1. [Reading a File in C#](https://www.ask-codey.com/lessons/reading-a-file-in-csharp) (Apprentice): Read a text file like a tiny database. Open it, go through it line by line with File.ReadLines or a StreamReader, split each CSV line into an object, and close it with using, all inside a try and catch.

## [Writing to Files in C#](https://www.ask-codey.com/courses/csharp-full-stack-bootcamp/topics/writing-to-files-in-csharp)

1. [Writing a File: StreamWriter's Defaults](https://www.ask-codey.com/lessons/writing-to-files-in-csharp-filestream-defaults) (Journeyman): A StreamWriter writes text to a file the way Console writes to the screen. By default it creates the file, or replaces one that's already there. Close it with using, so everything written reaches the disk.
2. [Appending to a File](https://www.ask-codey.com/lessons/writing-to-files-in-csharp-appending-to-a-file) (Journeyman): To add to the end of a file instead of replacing it, open a StreamWriter with append set to true, or use File.AppendAllText. Either creates the file if it isn't there yet.

## [Logging Exceptions in C#](https://www.ask-codey.com/courses/csharp-full-stack-bootcamp/topics/logging-exceptions-in-csharp)

1. [Logging Exceptions in C#: an overview](https://www.ask-codey.com/lessons/logging-exceptions-in-csharp-an-overview) (Journeyman): A log is a file of time-stamped lines recording what an app did and what went wrong, so you can find out later. Apps log to plain text files because they're fast, simple, and keep working when everything else fails.
2. [Creating the LogService](https://www.ask-codey.com/lessons/logging-exceptions-in-csharp-creating-the-logservice) (Journeyman): Build a LogService that appends one time-stamped line per entry to a file named for the day, such as logs\\errors\\20260314.log. It asks for an IClock so tests can choose the date, and it never lets a logging problem crash the app.
3. [Writing Log Entries](https://www.ask-codey.com/lessons/logging-exceptions-in-csharp-writing-log-entries) (Journeyman): Create one LogService for errors and one for ordinary events, each with its own folder. In a catch block, log the exception's type and message with the file or record involved, and tell the user something helpful.

## [Exercise: File I/O](https://www.ask-codey.com/courses/csharp-full-stack-bootcamp/topics/exercise-file-i-o)

1. [Exercise: File I/O](https://www.ask-codey.com/lessons/exercise-file-i-o) (Journeyman): Read a school's grades from a CSV file with mistakes in it, log every line you can't read with your own LogService, write an honor roll, and add new students to the end of a file.

Source: https://www.ask-codey.com/courses/csharp-full-stack-bootcamp/chapters/files-and-exceptions
