In this C# file handling tutorial series, you’ll learn how to perform essential file operations such as reading and writing various file formats including text, binary, and CSV.
At the end of the tutorial series, you’ll gain the expertise and knowledge needed to handle files and streams in the C# applications effectively.
What you’ll learn
- Reading various file formats including text, binary, and CSV.
- Monitor and respond to the changes in the file system using the file system watcher.
- Managing directories like creating, moving, renaming, and deleting.
Section 1. File Operations
In this section, you’ll learn how to perform common file operations such as copying a file, moving a file to a new location, and deleting a file.
- Path – shows you how to use the Path class to manage the file and directory paths effectively.
- Check if a file exists – shows you how to use the File.Exists() method.
- Copy a File – copies an existing file to a new file and overwrites if it exists.
- Move a file – learns how to move a specified file to a new location.
- Delete a file – shows you how to delete an existing file using the File.Delete() method.
Section 2. Working with Text Files
In this section, you’ll learn how to read an entire text file into a string, or an array of strings. If the text file is large, you’ll learn how to read it line by line using a stream. Also, you’ll learn how to write a text file effectively.
- Read text files – shows you various ways to read the contents of text files into a string or an array of strings.
- Write text files – shows you how to write data into a text file.
Section 3. Processing CSV files
Working with CSV (Comma-separated values) files is one of the most common tasks in data processing and analysis. By using CSV files, you can store tabular data simply, making them a widely used format for applications. This section shows you how to read and write CSV files effectively using the CSVHelper library.
- Read CSV files – shows you how to use the CsvHelper library to read CSV files with various options.
- Writing CSV files – learns how to use the CsvHelper library to write CSV files.
Section 4. Working with directories
In this section, you’ll learn how to directories effectively including creating, moving, and deleting directories within a file system.
- Checking if a directory exists – shows you how use check if a directory exists or not using the
Directory.Exists()
static method. - Creating a new directory – learns how to create a new directory if it does not exist using the
Directory.CreateDirectory()
static method. - Moving a directory – shows you how to move a directory and all of its contents to a new location using the
Directory.Move()
static method. - Deleting a directory – guides you on how to delete a directory and optionally all of its subdirectories and files using the
Directory.Delete()
static method. - Listing directory – shows you how to find subdirectories in a directory that match specified criteria.
- Listing files in a directory – learns how to list files that match specified criteria.
Section 5. Monitoring file system
This section shows you how to use the FileSystemWatcher class in C# to monitor file system changes in real time.
- Understanding file system watcher – introduces to you the FileSystemWatcher class and shows you how to respond to file system changes.