关闭

文件遍历器类

322人阅读 评论(0) 收藏 举报
/// <summary>
    /// 文件遍历器,不遍历子目录,这个用于遍历单目录
    /// </summary>
    class FilesFilder
    {
        /// <summary>
        /// 子成员,用于存储目录下的文件信息
        /// </summary>
        private Hashtable childElms;

        /// <summary>
        /// 遍历的根目录
        /// </summary>
        private String root;

        public String getPosition()
        {
            return root;
        }

       public String getFolderName()
        {
            String[] dirs = root.Split('//');
            return dirs[dirs.Length - 1];
        }

        /// <summary>
        /// 扩展名过滤器,存储需要的文件扩展名,若指定为空则不进行文件扩展名过滤
        /// </summary>
        private ArrayList filters;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="folder"></param>
        /// <param name="filters"></param>
        public FilesFilder(String folder, ArrayList filters)
        {
            childElms = new Hashtable();
            this.filters = filters;
            root = folder;
            DirectoryInfo dir = new DirectoryInfo(folder);
            tranversal(dir);
        }

        /// <summary>
        /// 遍历根目录下的文件
        /// </summary>
        /// <param name="dir"></param>
        private void tranversal(DirectoryInfo dir)
        {
            FileSystemInfo[] fsis = dir.GetFileSystemInfos();

            FileSystemInfo fsi;

            for (int i = 0; i < fsis.Length; i++)
            {
                fsi = fsis[i];

                if (fsi.GetType() == typeof(FileInfo) && isPassedFilters(fsi.Extension.ToString().ToUpper()))
                {
                    //childElms.Add(DirFileUtil.getFileNameWithoutPath(fsi.FullName));
                    childElms.Add(DirFileUtil.getFileNameWithoutPath(fsi.FullName),GetFilePrimaryInfo((FileInfo)fsi));
                }
            }
        }

       /// <summary>
        /// 判断是否通过了扩展名过滤器
        /// </summary>
        /// <param name="ext"></param>
        /// <returns></returns>
        private bool isPassedFilters(String ext)
        {
            if (filters == null) return true;
            if (filters.Count == 0) return true;
            ext = ext.Replace(".", "");

            return filters.Contains(ext);
        }

        /// <summary>
        /// 返回文件列表
        /// </summary>
        /// <returns></returns>
        public Hashtable getFiles()
        {
            return childElms;
        }

        /// <summary>
        /// 返回通过过滤的文件数量
        /// </summary>
        /// <returns></returns>
        public int getFileCount()
        {
            return childElms.Count;
        }

        private FilePrimaryInfo GetFilePrimaryInfo(FileInfo fsi)
        {
            FilePrimaryInfo info = new FilePrimaryInfo();

            info.Name = DirFileUtil.getFileNameWithoutPath(fsi.FullName);
            info.Path = fsi.FullName.Replace(info.Name,"");
            info.ModifyTime = fsi.LastWriteTime.ToString();
            info.Size = fsi.Length.ToString();

            return info;
        }
    }

    /// <summary>
    /// 文件夹遍历器,遍历子目录
    /// </summary>
    class FoldersFinder
    {
        /// <summary>
        /// 子成员
        /// </summary>
        private ArrayList childElms;

        /// <summary>
        /// 遍历的根目录
        /// </summary>
        private String root;

        /// <summary>
        /// 扩展名过滤器,存储需要的文件扩展名,若指定为空则不进行文件扩展名过滤
        /// </summary>
        private ArrayList filters;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="folder"></param>
        /// <param name="filters"></param>
        public FoldersFinder(String folder, ArrayList filters)
        {
            childElms = new ArrayList();
            root = folder;
            this.filters = filters;
            DirectoryInfo dir = new DirectoryInfo(folder);

            // 加入本身
            FilesFilder filesFilder = new FilesFilder(folder, filters);

            if (filesFilder.getFileCount() > 0)
            {
                childElms.Add(filesFilder);
            }

            tranversal(dir);
        }

        /// <summary>
        /// 遍历根目录下的子目录,将过滤过的文件夹加入
        /// </summary>
        /// <param name="dir"></param>
        private void tranversal(DirectoryInfo dir)
        {
            FileSystemInfo[] fsis = dir.GetFileSystemInfos();

            FileSystemInfo fsi;

            for (int i = 0; i < fsis.Length; i++)
            {
                fsi = fsis[i];

                if (fsi.GetType() == typeof(DirectoryInfo))
                {
                    FilesFilder filesFilder = new FilesFilder(fsi.FullName, filters);

                    if (filesFilder.getFileCount()>0)
                    {
                        childElms.Add(filesFilder);
                    }

                    tranversal((DirectoryInfo)fsi);
                }
            }
        }

        /// <summary>
        /// 返回目录列表
        /// </summary>
        /// <returns></returns>
        public ArrayList getFolders()
        {
            return childElms;
        }
    } 

2.一个辅助类,用于存储文件信息

    class FilePrimaryInfo : IComparable
    {
        private String name;
        private String path;
        private String size;
        private String modifyTime;

        private static String sortBy = "name";
        public static String SortBy
        {
            get { return sortBy; }
            set { sortBy = value; }
        }

        public int CompareTo(object obj)
        {
            if (!(obj is FilePrimaryInfo))
                throw new InvalidCastException("Not a valid Developer object.");

            FilePrimaryInfo filePrimaryInfo = (FilePrimaryInfo)obj;

            if (sortBy.Equals("NAME"))
            {
                return this.name.CompareTo(filePrimaryInfo.name);
            }
            else if (sortBy.Equals("SIZE"))
            {
                return this.size.CompareTo(filePrimaryInfo.size);
            }
            else if (sortBy.Equals("TIME"))
            {
                return this.modifyTime.CompareTo(filePrimaryInfo.modifyTime);
            }
            else
            {
                return this.name.CompareTo(filePrimaryInfo.name);
            }           
        }

        public String Name
        {
            get { return name; }
            set { name = value; }
        }

        public String Path
        {
            get { return path; }
            set { path = value; }
        }

        public String Size
        {
            get { return size; }
            set { size = value; }
        }

        public String ModifyTime
        {
            get { return modifyTime; }
            set { modifyTime = value; }
        }
    }

 

3.使用方法:

// 创建扩展名过滤器,过滤器的扩展名大小写要注意,你也可以修改

ArrayList filters=new ArrayList();

filters.Add("bmp");

filters.Add("jpg");

// 遍历单目录

FilesFilder finder = new FilesFilder(@"c:/abc", filters);

// 遍历目录及其子目录

FoldersFinder finder = new FoldersFinder(@"c:/abc", filters);

 
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:245233次
    • 积分:3373
    • 等级:
    • 排名:第10000名
    • 原创:21篇
    • 转载:306篇
    • 译文:3篇
    • 评论:42条