c#判断一个目录是否存在

sam 2021-07-17 14:47:54

思路

采用System.IO这个类库

函数

        /// <summary>
        /// 判断一个目录是否存在
        /// </summary>
        /// <param name="directoryPath"></param>
        /// <returns></returns>
        public static bool DirectoryIsExist(string path)
        {
            return Directory.Exists(path);
        }

使用

//判断一个目录是否存在
            String curDir = "d:\\Net5Publish";
            if (DirectoryIsExist(curDir))
            {
                Console.WriteLine("存在");
            }
            else
            {
                Console.WriteLine("不存在");
            }

备注

有些目录是存在的,但是没有访问的权限这时应用程序是会报异常的,所以,稳妥的做法,在判断的时候首先检查是否有访问的权限。

回帖
  • 消灭零回复
相关文章