C# 程式檢查目錄是否存在
使用 Directory.Exists 方法檢查目錄是否存在。
假設你需要檢查以下目錄是否存在 −
C:\Amit
為此,請使用 Exists() 方法 −
if (Directory.Exists("C:\Amit")) { Console.WriteLine("Directory Amit Exist!"); }
以下為完整程式碼 −
示例
using System.IO; using System; public class Program { public static void Main() { if (Directory.Exists("C:\Amit")) { Console.WriteLine("Directory Amit Exist!"); } else { Console.WriteLine("Directory Amit does not exist!"); } } }
輸出
Directory Amit does not exist!
廣告