DirectoryNotFoundException เกิดขึ้นเมื่อพยายามเข้าถึงไดเรกทอรีที่ไม่มีอยู่จริง (เช่น Directory.GetDirectories)
ตัวอย่างที่ทำให้เกิดข้อยกเว้น
using System;
using System.IO;
class Program
{
static void Main()
{
Directory.GetDirectories(@"D:\new\"); // ไดเรกทอรีไม่มีอยู่
}
}
ข้อความข้อยกเว้น
Unhandled Exception: System.IO.DirectoryNotFoundException: Could not find a part of the path
การป้องกัน: ตรวจสอบก่อนด้วย Directory.Exists(path) หรือ Try-Catch