Read along with the audio summary
Reading from a file before checking if it exists can trigger a File not found exception. This episode of the show, which came out on July 13, 2023, walks through the C sharp classes used for handling file operations. By the end, you will know how to locate, create, read, write, copy, and delete files using the File class, FileStream, StreamReader, and StreamWriter.
The File class handles everyday file operations in one place.
Writing code to manage files does not always require complex setups. Inside System dot I O, which is the part of C sharp used for input and output, there is a static class called the File class. It provides built in methods for common operations like creating, copying, deleting, reading, and writing to files, as well as appending text. For simple, straightforward tasks, such as reading or writing the entire contents of a small file at once, use the File class.
Once you know which class to use, there are two ways to specify the file directory path so your code can locate and save files.
An absolute directory points to the exact same spot every time.
Hardcoding a file location means leaving nothing to guesswork. An absolute directory is the full, explicit path starting from the root drive, such as C colon backslash Documents backslash Text dot txt. It tells the program the exact structure to follow from the very beginning of the drive.
In contrast, a relative directory makes your code more portable.
Relying on a specific hardcoded file structure can limit where your application works. A relative directory specifies the path relative to the current project's execution folder. In a C sharp project, this is typically the bin backslash Debug folder, which is where the program saves and looks for the file by default. Use relative paths when you want to make your application more portable.
The standard file workflow protects your system resources.
Opening a file is only part of the job. The standard procedure for interacting with a file is to open it, perform your read, write, or edit operation, and then close it. Closing the file is necessary to release the system resources it was using. Always check if a file exists before trying to read from it to prevent a File not found exception, and ensure you close your files when you are done.
FileStream gives you deeper control by working with bytes.
Sometimes the basic File class is not enough for the job. FileStream is a lower level class that provides a stream for reading from and writing to files by working with data as a sequence of bytes. Because it works at this lower level, it offers more control over file access and sharing than the File class.
Building on top of streams, StreamReader and StreamWriter make handling text much easier.
Working with raw bytes is not ideal when you just want to manage words. StreamReader and StreamWriter are built on top of streams to handle text better than a basic FileStream. They have more functionality, manage character encoding like U T F 8, and provide convenient methods like ReadLine and WriteLine. When you need more control, such as handling large files line by line or managing specific character encodings, use FileStream together with StreamReader for reading and StreamWriter for writing, and always close your streams, readers, and writers to release resources.
Matching the right C sharp tool and directory path to your task keeps your file operations clean and portable. Always check that a file exists before reading it, and close your streams, readers, and writers to release system resources.