代码改变世界

一个简单的目录遍历器

2006-09-10 09:40  Orin  阅读(524)  评论(1编辑  收藏  举报
        在程序中常常会用到遍历某一个目录,并对其进行一些操作,比如:搜索某一个特定文件,或者对该目录建立索引...如果每次都C&P遍历目录的代码,虽然代码不多,但让人感觉着实不爽,将其封装成可复用的类的确是不错的方法。
  1using System;
  2using System.Collections.Generic;
  3using System.Text;
  4using System.IO;
  5
  6namespace nView.Eidolon.Utility
  7{
  8    /// <summary>
  9    /// 目录信息事件参数
 10    /// </summary>

 11    public sealed class AccessDirectoryEventArgs : EventArgs
 12    {
 13        private DirectoryInfo dirInfo = null;
 14        /// <summary>
 15        /// 获取遍历到的目录信息
 16        /// </summary>

 17        public DirectoryInfo DirInfo
 18        {
 19            get return this.dirInfo; }
 20        }

 21
 22        private bool stop = false;
 23        /// <summary>
 24        /// 获取或设置一个值表示是否要求遍历器停止遍历
 25        /// </summary>

 26        public bool Stop
 27        {
 28            get return this.stop; }
 29            set this.stop = value; }
 30        }

 31
 32        /// <summary>
 33        /// 构造函数
 34        /// </summary>
 35        /// <param name="dirInfo"></param>

 36        public AccessDirectoryEventArgs(DirectoryInfo dirInfo)
 37        {
 38            this.dirInfo = dirInfo;
 39        }

 40    }

 41
 42    /// <summary>
 43    /// 文件信息事件参数
 44    /// </summary>

 45    public sealed class AccessFileEventArgs : EventArgs
 46    {
 47        private FileInfo fileInfo = null;
 48        /// <summary>
 49        /// 获取遍历到的文件信息
 50        /// </summary>

 51        public FileInfo FileInfo
 52        {
 53            get return this.fileInfo; }
 54        }

 55
 56        private bool stop = false;
 57        /// <summary>
 58        /// 获取或设置一个值表示是否要求遍历器停止遍历
 59        /// </summary>

 60        public bool Stop
 61        {
 62            get return this.stop; }
 63            set this.stop = value; }
 64        }

 65
 66        /// <summary>
 67        /// 构造函数
 68        /// </summary>
 69        /// <param name="dirInfo"></param>

 70        public AccessFileEventArgs(FileInfo dirInfo)
 71        {
 72            this.fileInfo = dirInfo;
 73        }

 74    }

 75
 76    /// <summary>
 77    /// 目录遍历器,用于遍历指定的目录
 78    /// </summary>

 79    public sealed class IntratorDirectory
 80    {
 81        // 停止遍历标志
 82        private bool stop = false;
 83
 84        private string path = string.Empty;
 85        /// <summary>
 86        /// 获取或设置需要遍历的路径
 87        /// </summary>

 88        public string Path
 89        {
 90            get return this.path; }
 91            set
 92            {
 93                if (value == null || value.Trim() == string.Empty)
 94                    throw new ArgumentNullException("Path""路经无效,请设置正确的路径");
 95                else
 96                    this.path = value;
 97            }

 98        }

 99
100        /// <summary>
101        /// 当遍历到文件夹时触发
102        /// </summary>

103        public event EventHandler<AccessDirectoryEventArgs> AccessDirectory = null;
104        /// <summary>
105        /// 当遍历到文件时触发
106        /// </summary>

107        public event EventHandler<AccessFileEventArgs> AccessFile = null;
108
109        /// <summary>
110        /// 创建一个目录遍历器的实例
111        /// </summary>
112        /// <param name="path">需要遍历的路径</param>

113        public IntratorDirectory(string path)
114        {            
115            this.Path = path;
116        }

117
118        /// <summary>
119        /// 创建一个目录遍历器的实例
120        /// </summary>

121        public IntratorDirectory()
122        { }
123
124        /// <summary>
125        /// 开始遍历
126        /// </summary>

127        public void Intrating()
128        {
129            this.stop = false;
130            this.Intrating(new DirectoryInfo(this.path));    
131        }

132
133        /// <summary>
134        /// 遍历目录
135        /// </summary>
136        /// <param name="fsi"></param>

137        private void Intrating(FileSystemInfo fsi)
138        {
139            if (this.stop)
140                return;
141
142            if (!fsi.Exists)
143                throw new ArgumentNullException("fsi""此文件系统信息不存在");
144
145            if (fsi is DirectoryInfo)
146            {
147                DirectoryInfo dirInfo = (DirectoryInfo)fsi;
148                if (AccessDirectory != null)
149                {
150                    AccessDirectoryEventArgs adea = new AccessDirectoryEventArgs(dirInfo);
151                    AccessDirectory(this, adea);
152                    this.stop = adea.Stop;
153                }

154
155                FileSystemInfo[] fsis = dirInfo.GetFileSystemInfos();
156                int length = fsis.Length;
157                for (int i = 0; i < length; i++)
158                    Intrating(fsis[i]);
159            }

160            else if (fsi is FileInfo)
161            {
162                if (AccessFile != null)
163                {
164                    AccessFileEventArgs afea = new AccessFileEventArgs((FileInfo)fsi);
165                    AccessFile(this, afea);
166                    this.stop = afea.Stop;
167                }

168            }

169        }

170    }

171}