C# – Get temp folder path and create a temp file

You can use Path.GetTempPath() to get the user’s temp folder path. Here’s an example: I’m running this in Windows, so it outputs my temp folder path: Path.GetTempPath() gets the temp folder path by checking environment variables (TMP, TEMP, USERPROFILE). It falls back to returning the system temp folder. Create a temp file Once you have … Read more

C# – How to update a file’s contents

There are three ways to update a file’s content: Which option you pick depends on the file’s format and size. For example, if you’re writing to an existing CSV file, you’d append new lines to the end of the file. If you’re updating a JSON file, you’d read all the JSON content, make changes, then … Read more

C# – How to read a text file

The simplest way to read a text file is by using a high-level method in the .NET File API (in System.IO), such as File.ReadAllText(). These high-level methods abstract away the details of opening a file stream, reading it with StreamReader, and closing the file. Here’s an example of reading a text file’s content into a … Read more

C# – How to programmatically update the User Secrets file

User Secrets are stored in secrets.json. This file is specific to your application. Once you know the path of secrets.json, you can load and update it. Here’s an example of how to update secrets.json programmatically: Note: 1) For brevity, this isn’t showing all using statements. 2) This is using Newtonsoft because it’s better than System.Text.Json … Read more

C# – Unit testing code that does File IO

If your code does File IO, such as reading text from a file, then it’s dependent on the file system. This is an external dependency. In order to make the unit tests fast and reliable, you can mock out the external dependencies. To mock out the file system dependency, you can wrap the File IO … Read more

C# – How to update appsettings.json programmatically

You have to overwrite the appsettings.json file to be able to update values programmatically. You have to deal with the whole file, not individual parts of it. The process can be summarized in the following steps: There are two options for deserialization. You can either 1) Deserialize appsettings.json into a dynamic object or 2) Load … Read more

WinForms – How to prompt the user for a file

In a WinForms project, you can prompt the user to select a file by using the OpenFileDialog control: When you call ShowDialog(), it’ll prompt the user to select a file: When the user clicks Open, you’ll be able to get the file path they selected from the OpenFileDialog.FileName property. To use the OpenFileDialog control, drag … Read more

C# – How to use FileSystemWatcher

You can use the FileSystemWatcher class to detect file system changes, such as when a file is created, deleted, modified, or renamed. When a change happens, it raises an event that you can handle. This is an event-based alternative to polling for file changes. In this article, I’ll show how to use FileSystemWatcher to detect … Read more

How to use relative paths in a Windows Service

Relative paths are resolved relative to the current working directory. When you’re running a Windows Service, the default working directory is C:\Windows\system32 or C:\Windows\SysWOW64. Therefore relative paths are resolved from these system folders, which can lead to problems when read/writing files. Here are the most common problems you’ll run into: System.IO.DirectoryNotFoundException: Could not find a … Read more