Directory.Exists()方法用于判斷指定的文件夾是否存在。該方法接受一個字符串參數(shù),表示文件夾的路徑,返回一個布爾值,如果文件夾存在則返回true,否則返回false。
以下是一個示例代碼:
using System;
using System.IO;
class Program
{
static void Main()
{
string folderPath = @"C:\Temp"; // 指定文件夾路徑
bool folderExists = Directory.Exists(folderPath); // 使用Directory.Exists()判斷文件夾是否存在
if (folderExists)
{
Console.WriteLine("文件夾存在");
}
else
{
Console.WriteLine("文件夾不存在");
}
}
}
注意:在使用Directory.Exists()判斷文件夾是否存在時,需要提供文件夾的完整路徑。