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# – Get all files in a folder

There are two simple ways to get all files in a folder: I’ll show examples below, along with a few other scenarios, such as getting files from subfolders. Note: ‘directory’ and ‘folder’ mean the same thing. I use these terms interchangeably. Get all files with Directory.GetFiles() Directory.GetFiles() returns all file paths from the top-level folder … Read more

C# – Delete all files in a directory

Use Directory.EnumerateFiles() to get the file paths in a directory. Then loop over the file paths and delete each file. Here’s an example: This deletes the root directory’s files without deleting the directory itself. I’ll show more examples. Delete all files with a specific extension Use Directory.EnumerateFiles’ searchPattern pattern to search for files with a … Read more

C# – How to make a file read-only

There are two ways to programmatically make a file read-only: Here’s an example showing both ways to make a file read-only: Using File.SetAttributes() is useful when you want to manage all of file’s attributes at once. FileAttributes is an enum flag, so you can use bitwise operations to add/remove multiple attributes at once. That’s why … Read more

C# – How to set permissions for a directory (Windows only)

When you want to set permissions for a directory (and its files/subdirectories), you can use DirectoryInfo.GetAccessControl() to get the directory’s security, add/modify/remove access control rules, and then use DirectoryInfo.SetAccessControl() to apply the changes. Access control rules are a complex combination of different settings. They control being able to do things like creating a file in … Read more

C# – How to delete a directory

The simplest way to delete a directory is by using Directory.Delete() (in System.IO). Here are a few examples: You have to specify the path of the directory to delete. This can be an absolute or relative path. You can pass in a recursive flag, which tells it to delete everything in the directory (files and … Read more

C# – How to create directories

You can use Directory.CreateDirectory() to create a directory at the specified path (absolute or relative), like this: This creates the directory if it doesn’t exist, otherwise it does nothing. That means you don’t need to check if the directory exists before calling Directory.CreateDirectory(). In this article, I’ll go into more details about using Directory.CreateDirectory(). CreateDirectory() … Read more

C# – Search for files in a directory

Use Directory.EnumerateFiles() to search for files in a directory and then loop over the file paths (and then reading the files, or whatever you want to do with the info). Here’s an example of searching for files containing “hello” in the file name: Note: Use the wildcard character * to match anything. It returns an … 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 create a file and write to it

There are a few ways to create a file and write to it using the .NET File API (in System.IO). The simplest way is to use high-level methods like File.WriteAllText() and File.WriteAllLines(), specifying the file path and string(s) to write to the file. Here’s an example of using these (and their async equivalents): These high-level … Read more

C# – How to delete a file

You can use File.Delete() (in System.IO) to delete a file by specifying its relative or absolute path. Here’s an example: Note: Unlike other methods in the File API, there’s no async version of File.Delete(). If the specified file exists and the permissions are right, then File.Delete() deletes the file as expected. If there’s a problem, … Read more

C# – Check if a directory is empty

The simplest way to check if a directory is empty is by calling Directory.EnumerateFileSystemEntries(). If this doesn’t return anything, then the directory doesn’t have any files or subdirectories (folders), which means it’s empty. Here’s an example: Another situation to consider is when a directory has no files but might have empty subdirectories (folders). You can … 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