File operations in .NET: Create, Read, Update, Delete, and Search

.NET has a good, high-level File API that abstracts away the complexity of file operations. You can create, read, update, delete, and search for files with a single method call. It’s flexible and provides multiple approaches, allowing you to pick the best tool for the job. For example, you can load an entire file all … Read more

C# – Find all empty folders

An empty folder: Has no files. Either has no folders, or has folders that are empty. In this article I’ll show code that finds empty folders based on this definition. Code that finds empty folders Given a root folder path, the following code recursively looks for empty folders and writes the empty folder path to … Read more

Event-driven .NET: use FileSystemWatcher instead of polling for new files

You can use FileSystemWatcher to get notified of file system changes, such as when a file is created. Here’s an example of reacting to when a JSON file is created: Compared to polling, this event-driven approach with FileSystemWatcher is more efficient and simpler. In the remainder of this article I’ll show an example of how … Read more

C# – How to unit test async methods

Let’s say you have the following async method you want to test: Here’s how to unit test this: This is awaiting the method you’re testing. To await it, you must make the unit test method return async Task. This example is a bit simplistic. In the real world when you are working with async methods, … Read more