递归循环删除目录(包括删除自己)

2014-06-27 12:48:46  访问(2019) 赞(0) 踩(0)


        /// 
        /// 递归循环删除目录(包括删除自己)
        /// 
        /// 
        public static void DeleteDir(string dirName)
        {
            if (dirName == null || dirName.Length == 0)
                throw ThrowSlowXFunctionsExceptions.NewSlowXFunctionsExceptionParameterIsNullOrEmptyAndBlankMessage(MethodBase.GetCurrentMethod(), "string dirName");

            // 目录不存在,返回 //
            if (!Directory.Exists(dirName))
                return;

            //针对当前目录建立目录引用对象
            DirectoryInfo dirInfo = new DirectoryInfo(dirName);

            FileSystemInfo[] fileSystemInfo = dirInfo.GetFileSystemInfos();

            if (!(fileSystemInfo == null || fileSystemInfo.Length == 0))
            {
                //循环判断当前目录下的文件和目录
                foreach (FileSystemInfo fsi in fileSystemInfo)
                {
                    //如果是文件
                    if (fsi is DirectoryInfo)
                    {
                        DeleteDir(fsi.FullName);
                    }
                    else
                    {
                        File.Delete(fsi.FullName);
                    }
                }
            }

            Directory.Delete(dirInfo.FullName);

        }


上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)