C#使用WIN32API来遍历文件和目录[3] #region 控制对象特性的一些属性 **************************************** private bool bolThrowIOException = true; /// <summary> /// 发生IO错误时是否抛出异常 /// </summary> public bool ThrowIOException { get { return this.bolThrowIOException; } set { this.bolThrowIOException = value; } } private bool bolReturnStringType = true; /// <summary> /// 是否以字符串方式返回查询结果,若返回true则当前对象返回为字符串, /// 否则返回 System.IO.FileInfo或System.IO.DirectoryInfo类型 /// </summary> public bool ReturnStringType { get { return bolReturnStringType; } set { bolReturnStringType = value; } } private string strSearchPattern = "*"; /// <summary> /// 要匹配的文件或目录名,支持通配符 /// </summary> public string SearchPattern { get { return strSearchPattern; } set { strSearchPattern = value; } } private string strSearchPath = null; /// <summary> /// 搜索的父目录,必须为绝对路径,不得有通配符,该目录必须存在 /// </summary> public string SearchPath { get { return strSearchPath; } set { strSearchPath = value; } } private bool bolSearchForFile = true; /// <summary> /// 是否查找文件 /// </summary> public bool SearchForFile { get { return bolSearchForFile; } set { bolSearchForFile = value; } } private bool bolSearchForDirectory = true; /// <summary> /// 是否查找子目录 /// </summary> public bool SearchForDirectory { get { return bolSearchForDirectory; } set { bolSearchForDirectory = value; } } #endregion /// <summary> /// 关闭对象,停止搜索 /// </summary> public void Close() { this.CloseHandler(); } #region IEnumerator 成员 ********************************************** /// <summary> /// 返回当前对象 /// </summary> public object Current { get { return objCurrentObject ; } } /// <summary> /// 找到下一个文件或目录 /// </summary> /// <returns>操作是否成功</returns> public bool MoveNext() { bool success = false; while (true) { if (this.bolStartSearchFlag) success = this.SearchNext(); else success = this.StartSearch(); if (success) { if (this.UpdateCurrentObject()) return true; } else { this.objCurrentObject = null; return false; } } } /// <summary> /// 重新设置对象 /// </summary> public void Reset() { if (this.strSearchPath == null) throw new System.ArgumentNullException("SearchPath can not null"); if (this.strSearchPattern == null || this.strSearchPattern.Length == 0) this.strSearchPattern = "*"; this.intSearchedCount = 0; this.objCurrentObject = null; this.CloseHandler(); this.bolStartSearchFlag = false; this.bolIsEmpty = false; this.intLastErrorCode = 0; } #endregion |