Working with Files and Streams in C#

Understanding file and data manipulation is key to a full understanding of C#. This course will teach you how to manage files and directories, read and write data in a number of different ways, and write unit tests using a mock file system.
Course info
Rating
(25)
Level
Intermediate
Updated
Oct 12, 2018
Duration
3h 13m
Table of contents
Course Overview
Managing Files and Directories
Monitoring the File System for Changes
Reading and Writing Entire Files into Memory
Reading and Writing Data Incrementally Using Streams
Reading and Writing CSV Data
Creating Automated Tests for File Access Code
Description
Course info
Rating
(25)
Level
Intermediate
Updated
Oct 12, 2018
Duration
3h 13m
Description

At the core of C# file IO is a thorough knowledge of how to manipulate files and directories and use streams. In this course, Working with Files and Streams in C#, you will learn the skills you need to read and write text, binary, and CSV files, and work with streams of data. First, you will learn how to manipulate files and directories on disk. Next, you will discover how to respond to changes in the file system and read and write data by loading entire files into memory or by incrementally processing them with streams. Finally, you will explore how to write testable file IO code and how to use a mock file system to create faster-executing and more maintainable tests. When you’re finished with this course, you will have a foundational understanding of file access and stream manipulation code that will help you immensely as you move forward and create your own file- or stream-manipulating applications.

About the author
About the author

With over 15 years experience, Jason Roberts is a Microsoft .NET MVP, freelance developer, and author.

More from the author
Mocking with Moq and NUnit
Intermediate
1h 53m
Feb 22, 2019
Introduction to .NET Testing with NUnit 3
Intermediate
2h 6m
Dec 17, 2018
More courses by Jason Roberts
Section Introduction Transcripts
Section Introduction Transcripts

Course Overview
Hi everyone. My name's Jason Roberts. Welcome to my course Working with Files and Streams in C#. I'm a freelance developer and author. In this course, you're going to learn the different ways to work with files and file data. Some of the major topics that we'll cover in this course include how to manipulate files and directories on the file system, how to read and write text binary and CSV files, how to use streams, and how to write automated tests for file access code. By the end of this course, you'll understand how to read data files into your C# application and how to write output data files from it. Before beginning the course, it's recommended that you have at least a basic understanding of C#. I hope you'll join me on this journey with the Working with Files and Streams in C# course at Pluralsight.

Managing Files and Directories
Hi everyone. My name's Jason Roberts. Welcome to this course Working with Files and Streams in C#. In this first module, we're going to be learning about how to manage files and directories from C# code. So, we're going to kick off this module by creating a new console app that we'll be using throughout the rest of this course. And we're going to use this console app to read in input files, do some processing on them, and then write them out again. As part of this processing, we're going to learn how to check if a file exists on the file system, we're going to learn how to check if a directory exists, we'll see how to create a new directory from code, how to copy a file, how to get the file extension from a given file name, how to move a file, how to delete a directory, and also how we can specify a directory path and find all of the files in that directory. We'll also learn that when we do this we can specify an optional search filter to limit the list of files that we get back. This course is organized into a number of modules. We're currently in the managing files and directories module. Following this module, we're going to learn how we can monitor the file system and react to changes in it. In the next module, we'll learn how we can read and write entire files into memory to process them. And we'll then learn how instead of reading the entire file into memory we can process chunks of files by using streams. In the following module, we'll learn how we can read and write CSV data files. And finally, we'll learn how we can create automated tests for code that accesses the file system, and we'll learn how we can actually abstract away the file system to make writing tests easier. So without further ado, we'll head over to Visual Studio now and we'll go and create the console app.

Reading and Writing Entire Files into Memory
Hi. Welcome back. In the previous module, we learned how to use the FileSystemWatcher to respond to changes in the file system. Currently, the application doesn't actually do any processing of the input files. And in this module, we're going to add functionality to actually perform some processing on the detected input files. So in this module, we're going to start off by learning how we can read entire text files into an in-memory string representation. We'll also learn how we can write an entire string to a new file. We'll also learn how we can read an entire text file into memory, but rather than into a single string object we get each line in a separate string array element. And we'll also learn if we have an array of strings how we can write this out to a new text file on disk. When we're reading and writing text files, we can specify text file encodings. So we'll have a look at this. And we'll also learn how we can quickly append text to an existing text file. The console application that we're building needs to support not only text files, but also binary files. So we'll learn how we can read an entire binary file into memory and also how we can write binary data to a new file on disk. By the end of this module, the application will be able to process text and binary files, but it's going to do this by reading the entire contents of these files into memory, doing some processing on them, and then writing out some output. There are some benefits and drawbacks to this approach so we'll discuss some of these considerations. So without further ado, let's head over to Visual Studio and we'll learn how we can read the entire contents of a text file into a string.

Reading and Writing Data Incrementally Using Streams
Hi. Welcome back. In the previous module, we learned how to manipulate text and binary file content by loading the content into memory, performing some processing, and then writing out to a new file. In this module, we're going to learn how we can instead read and write data incrementally using streams. This means that we don't have to load the entire file into memory to be able to process it. So, we're going to kick off this module by getting a high-level introduction to what streams are and how they work. And we'll also look at some of the potential benefits of using streams over loading the entire file into memory. We'll learn about the. NET classes involved in stream manipulation. And then we'll head over to Visual Studio and learn how to read and write text data using streams. We'll also learn how we can selectively process part of a text stream, and then we'll also learn how we can use streams to read and write binary data. When we're working with binary data, we can also make use of a couple of classes, the BinaryReader and BinaryWriter classes, so we'll learn about both of these. In the previous module, we'll learned about text encodings, and in this module we'll learn how we can specify encodings when working with streams. We'll learn how to append data to existing files using streams, how to perform random file access when using a file stream, and finally, how to make use of in-memory streams. So, let's kick off this module by getting a high-level introduction to streams.

Reading and Writing CSV Data
Hi. Welcome back. In the previous module, we learned how to use streams to read and write text and binary data. In this module, we're going to be completing the functionality of the console app by allowing it to read and process CSV data. So, in this module we're going to start off with a brief overview of the CsvHelper library that we'll be using in this module. And we'll learn how to use this library to, first off, read CSV file data into dynamic C# objects. We'll also learn how to configure different options when we're reading CSV data, such as the ability to trim spaces, allow CSV comments, ignore blank lines, how to configure custom delimiters, so for example using semicolons rather than commas, and also how to configure the presence of CSV file headers. We will then learn that in addition to reading data into dynamic objects, we can actually read them into strongly-typed entity classes. We'll learn how we can do this automatically and also how we can configure custom class mappings and also how we can further customize the reading of CSV data by creating custom type converters. And finally, we'll learn how to use the CsvHelper library to write out CSV files. So, let's kick off this module by getting a brief overview of the CsvHelper library itself.

Creating Automated Tests for File Access Code
Hi. Welcome back. In the previous module, we learned how to read and write CSV data. So now, our console application supports binary, text, and CSV file processing. Up until this point, however, we've been testing the results of this processing by manually checking the output directory. In this module, we're going to learn how we can create automated tests that read and write to a mock file system. So, in this module we're going to start off by looking at some of the problems associated with testing file access code and then we'll get an overview of the System. IO. Abstractions NuGet package that we'll be using in this module. We'll then go and refactor some of our code to make it easier to test. And once this refactoring is complete we'll go and add a test project to our solution. And we'll start off by creating a TextFileProcessor test. We'll then add a test for our BinaryFileProcessor class and a test for our CsvFileProcessor. And finally, we'll learn how we can use the ApprovalTests testing library to make asserting mock file output easier. So let's kick off this module by looking at some of the problems associated with testing file access code.