
What could cause the DirectoryNotFoundException in a C# program? DirectoryNotFoundException is a type of file handling exception that is caused by a missing directory.
This C# article explains the DirectoryNotFoundException type.
In this example, we call the Directory.GetDirectories method on a directory that does not happen to exist on the computer. On my computer, the directory "lottery-numbers" does not exist because I unfortunately do not have any winning numbers. Thus, an exception is thrown and the program terminates.
Program that causes DirectoryNotFoundException [C#]
using System.IO;
class Program
{
static void Main()
{
Directory.GetDirectories("C:\\lottery-numbers\\");
}
}
Output
Unhandled Exception: System.IO.DirectoryNotFoundException: Could not find a part
of the path 'C:\lottery-numbers\'....
How to fix it? You can fix the DirectoryNotFoundException in different ways depending on what the problem is. If the software simply requires a certain directory, the easiest thing to do would be to create that directory manually.

Through the type derivation system, exceptions in the .NET Framework tell us fairly clearly what went wrong. A more generic IOException would be confusing in this program, but the DirectoryNotFoundException is crystal clear.
Exception Handling