C#中如何判断目录是否存在?_c#当前目录

C#中如何判断目录是否存在?_c#当前目录

编码文章call10242025-02-19 10:28:398A+A-

在 C# 中,可以使用 System.IO 命名空间中的 Directory.Exists 方法来判断目录是否存在。该方法会检查指定路径下的目录是否存在,并返回一个布尔值(true 表示目录存在,false 表示目录不存在)。

使用Directory.Exists判断目录是否存在

示例:

using System;
using System.IO;

class Program
{
    static void Main()
    {
        string directoryPath = @"C:\exampleDirectory"; // 要检查的目录路径

        // 判断目录是否存在
        if (Directory.Exists(directoryPath))
        {
            Console.WriteLine("Directory exists.");
        }
        else
        {
            Console.WriteLine("Directory does not exist.");
        }
    }
}

说明:

  • Directory.Exists(directoryPath) 方法检查指定路径的目录是否存在。
  • 如果目录存在,返回 true,否则返回 false。

总结:

  • 使用 Directory.Exists 方法来判断目录是否存在。
  • 该方法适用于检查文件夹路径的存在性,返回布尔值 true 或 false。
点击这里复制本文地址 以上内容由文彬编程网整理呈现,请务必在转载分享时注明本文地址!如对内容有疑问,请联系我们,谢谢!
qrcode

文彬编程网 © All Rights Reserved.  蜀ICP备2024111239号-4