using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace iWare_SCADA_BusinessLogical.Utils { /// /// FindFiles 实时查找文件 /// /// /// FindFiles e = new FindFiles(); /// e.SearchPath = @"c:\"; /// e.ReturnStringType = true ; /// e.SearchPattern = "*.exe"; /// e.SearchForDirectory = false ; /// e.SearchForFile = true; /// e.SearchCreationTime = DateTime.Now.AddDays(-1); /// foreach (object name in e) /// { /// System.Console.WriteLine(name); /// } /// System.Console.ReadLine(); /// /// public class FindFiles : System.Collections.IEnumerable { private bool bolReturnStringType = true; /// /// 如果为True返回文件名,否则返回 System.IO.FileInfo 或 System.IO.DirectoryInfo /// public bool ReturnStringType { get { return bolReturnStringType; } set { bolReturnStringType = value; } } /// /// 查询文件创建开始时间 /// public DateTime? SearchCreationTime { get; set; } /// /// 查询文件创建结束时间 /// public DateTime? SearchCreationEndTime { get; set; } /// /// 查询文件修改开始时间 /// public DateTime? SearchModifyTime { get; set; } /// /// 查询文件修改结束时间 /// public DateTime? SearchModifyEndTime { get; set; } private string strSearchPattern = "*"; /// /// 查询条件 /// public string SearchPattern { get { return strSearchPattern; } set { strSearchPattern = value; } } private string strSearchPath = null; /// /// 查询路径 /// public string SearchPath { get { return strSearchPath; } set { strSearchPath = value; } } private bool bolSearchForFile = true; /// /// 查询文件 /// public bool SearchForFile { get { return bolSearchForFile; } set { bolSearchForFile = value; } } private bool bolSearchForDirectory = true; /// /// 查询文件夹 /// public bool SearchForDirectory { get { return bolSearchForDirectory; } set { bolSearchForDirectory = value; } } private bool bolThrowIOException = true; /// /// 是否抛出异常 /// public bool ThrowIOException { get { return this.bolThrowIOException; } set { this.bolThrowIOException = value; } } /// /// IEnumerator接口 /// /// public System.Collections.IEnumerator GetEnumerator() { FileDirectoryEnumerator e = new FileDirectoryEnumerator(); e.ReturnStringType = this.bolReturnStringType; e.SearchForDirectory = this.bolSearchForDirectory; e.SearchForFile = this.bolSearchForFile; e.SearchPath = this.strSearchPath; e.SearchPattern = this.strSearchPattern; e.ThrowIOException = this.bolThrowIOException; e.SearchCreationTime = this.SearchCreationTime; e.SearchCreationEndTime = this.SearchCreationEndTime; e.SearchModifyTime = this.SearchModifyTime; e.SearchModifyEndTime = this.SearchModifyEndTime; myList.Add(e); return e; } /// /// 关闭对象 /// public void Close() { foreach (FileDirectoryEnumerator e in myList) { e.Close(); } myList.Clear(); } private System.Collections.ArrayList myList = new System.Collections.ArrayList(); } }