• C#利用DirectoryInfo遍历文件夹所有文件的类


    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;//
    using System.Collections;//
    
    namespace ControlFile
    {
        /// <summary>
        /// 文件操作类
        /// </summary>
        public class ControlFileClass
        {
             //字段声明
            private ArrayList fileListPath = new ArrayList();
            private ArrayList fileListName = new ArrayList();
    
            /// <summary>
            /// 文件路径
            /// </summary>
            public ArrayList FileListPath
            {
                get { return fileListPath; }
            }
    
            /// <summary>
            /// 文件名称
            /// </summary>
            public ArrayList FileListName
            {
                get { return fileListName; }
            }
    
            /// <summary>
            /// 构造函数并遍历文件夹获取文件名称,路径
            /// </summary>
            /// <param name="sourceDirectory">文件夹路径</param>
            /// <param name="nextFold">是否继续查找更深路径</param>
            public ControlFileClass(string sourceDirectory, bool nextFold)
            {
                DirectoryInfo diSource = new DirectoryInfo(sourceDirectory);
                GetAllList(diSource,nextFold);
            }
    
            public void GetAllList(DirectoryInfo source, bool nextFold)
            {
                try
                {
                    foreach (FileInfo fi in source.GetFiles())
                    {
                        fileListPath.Add(fi.FullName);
                        fileListName.Add(fi.Name);
                    }
                    if (nextFold)//如果设置为可以向更深目录遍历则遍历
                    {
                        foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
                        {
                            GetAllList(diSourceSubDir, nextFold);
                        }
                    }
                    else//如果设置为不向更深目录遍历则直接用文件夹表示
                    {
                        //遍历获取文件夹
                        foreach (DirectoryInfo d in source.GetDirectories())
                        {
                            fileListPath.Add(d.FullName);
                            fileListName.Add(d.Name);
                        }
                    }
                }
                catch
                { }
            }
    
            /// <summary>
            /// 判断一个路径是文件还是文件夹
            /// </summary>
            /// <param name="fileName">文件路径</param>
            /// <returns>true:文件夹,false:文件</returns>
            public static bool IsFolder(string fileNamePath)
            {
                FileInfo fileInfo = new FileInfo(fileNamePath);
                if ((fileInfo.Attributes & FileAttributes.Directory) != 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
    
            /// <summary>
            /// 删除指定文件夹
            /// </summary>
            /// <param name="dir">文件夹路径</param>
            public static void DeleteFolder(string dirPath)
            {
                foreach (string d in Directory.GetFileSystemEntries(dirPath))
                {
                    if (File.Exists(d))
                    {
                        FileInfo fi = new FileInfo(d);
                        if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1)
                            fi.Attributes = FileAttributes.Normal;
                        File.Delete(d);//直接删除其中的文件   
                    }
                    else
                    {
                        DeleteFolder(d);//递归删除子文件夹   
                    }
                }
                Directory.Delete(dirPath);//删除已空文件夹   
            }
        }
    }
  • 相关阅读:
    Spring AOP日志实现(一)
    ArrayList和LinkedList的区别
    webmagic保存数据
    Java静态代码块、构造代码块执行顺序问题
    为Spring Cloud Ribbon配置请求重试(Camden.SR2+)
    centos关机与重启命令
    解决win环境下访问本机虚拟机中centos7 ftp服务器的问题
    【MongoDB异常】Exception authenticating MongoCredential解决方法
    mongodb解决只能本地连接不能远程连接问题
    Windows Server 2008 R2提示api-ms-win-crt-runtime-l1-1-0.dll 丢失解决方法
  • 原文地址:https://www.cnblogs.com/mane/p/1917961.html
Copyright © 2020-2023  润新知