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 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

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