C#使用WIN32API来遍历文件和目录[2]
private System.Collections.ArrayList myList = new System.Collections.ArrayList();
}//public class FileDirectoryEnumerable : System.Collections.IEnumerable
///
/// 文件和目录的遍历器
///
/// 本对象为Win32API函数 FindFirstFile , FindNextFile
/// 和 FindClose 的一个包装
///
/// 以下代码演示使用了 FileDirectoryEnumerator
///
/// FileDirectoryEnumerator e = new FileDirectoryEnumerator();
/// e.SearchPath = @"c:";
/// e.Reset();
/// e.ReturnStringType = true ;
/// while (e.MoveNext())
/// {
/// System.Console.WriteLine
/// ( e.LastAccessTime.ToString("yyyy-MM-dd HH:mm:ss")
/// + " " + e.FileLength + " t" + e.Name );
/// }
/// e.Close();
/// System.Console.ReadLine();
///
/// 编写 袁永福 ( http://www.xdesigner.cn )2006-12-8
public class FileDirectoryEnumerator : System.Collections.IEnumerator
{
#region 表示对象当前状态的数据和属性 **********************************
///
/// 当前对象
///
private object objCurrentObject = null;
private bool bolIsEmpty = false;
///
/// 该目录为空
///
public bool IsEmpty
{
get { return bolIsEmpty; }
}
private int intSearchedCount = 0;
///
/// 已找到的对象的个数
///
public int SearchedCount
{
get { return intSearchedCount; }
}
private bool bolIsFile = true;
///
/// 当前对象是否为文件,若为true则当前对象为文件,否则为目录
///
public bool IsFile
{
get { return bolIsFile; }
}
private int intLastErrorCode = 0;
///
/// 最后一次操作的Win32错误代码
///
public int LastErrorCode
{
get { return intLastErrorCode; }
}
///
/// 当前对象的名称 字串2
///
public string Name
{
get
{
if (this.objCurrentObject != null)
{
if (objCurrentObject is string)
return (string)this.objCurrentObject;
else
return ((System.IO.FileSystemInfo)this.objCurrentObject).Name;
}
return null;
}
}
///
/// 当前对象属性
///
public System.IO.FileAttributes Attributes
{
get { return (System.IO.FileAttributes)myData.dwFileAttributes; }
}
///
/// 当前对象创建时间
///
public System.DateTime CreationTime
{
get
{
long time = ToLong(myData.ftCreationTime_dwHighDateTime, myData.ftCreationTime_dwLowDateTime);
System.DateTime dtm = System.DateTime.FromFileTimeUtc(time);
return dtm.ToLocalTime();
}
}
///
/// 当前对象最后访问时间
///
public System.DateTime LastAccessTime
{
get
{
long time = ToLong(myData.ftLastAccessTime_dwHighDateTime, myData.ftLastAccessTime_dwLowDateTime);
System.DateTime dtm = System.DateTime.FromFileTimeUtc(time);
return dtm.ToLocalTime();
}
}
///
/// 当前对象最后保存时间
///
public System.DateTime LastWriteTime
{
get
{
long time = ToLong(myData.ftLastWriteTime_dwHighDateTime, myData.ftLastWriteTime_dwLowDateTime);
System.DateTime dtm = System.DateTime.FromFileTimeUtc(time);
return dtm.ToLocalTime();
}
}
///
/// 当前文件长度,若为当前对象为文件则返回文件长度,若当前对象为目录则返回0
///
public long FileLength
{
get
{
if (this.bolIsFile)
return ToLong(myData.nFileSizeHigh, myData.nFileSizeLow);
else
return 0;
}
}
#endregion
}//public class FileDirectoryEnumerable : System.Collections.IEnumerable
///
/// 文件和目录的遍历器
///
///
/// 和 FindClose 的一个包装
///
/// 以下代码演示使用了 FileDirectoryEnumerator
///
/// FileDirectoryEnumerator e = new FileDirectoryEnumerator();
/// e.SearchPath = @"c:";
/// e.Reset();
/// e.ReturnStringType = true ;
/// while (e.MoveNext())
/// {
/// System.Console.WriteLine
/// ( e.LastAccessTime.ToString("yyyy-MM-dd HH:mm:ss")
/// + " " + e.FileLength + " t" + e.Name );
字串7
/// }
/// e.Close();
/// System.Console.ReadLine();
///
/// 编写 袁永福 ( http://www.xdesigner.cn )2006-12-8
public class FileDirectoryEnumerator : System.Collections.IEnumerator
{
#region 表示对象当前状态的数据和属性 **********************************
///
/// 当前对象
///
private object objCurrentObject = null;
private bool bolIsEmpty = false;
///
/// 该目录为空
///
public bool IsEmpty
{
get { return bolIsEmpty; }
}
private int intSearchedCount = 0;
字串9
///
/// 已找到的对象的个数
///
public int SearchedCount
{
get { return intSearchedCount; }
}
private bool bolIsFile = true;
///
/// 当前对象是否为文件,若为true则当前对象为文件,否则为目录
///
public bool IsFile
{
get { return bolIsFile; }
}
private int intLastErrorCode = 0;
///
/// 最后一次操作的Win32错误代码
///
public int LastErrorCode
{
get { return intLastErrorCode; }
}
///
/// 当前对象的名称 字串2
///
public string Name
{
get
{
if (this.objCurrentObject != null)
{
if (objCurrentObject is string)
return (string)this.objCurrentObject;
else
return ((System.IO.FileSystemInfo)this.objCurrentObject).Name;
}
return null;
}
}
///
/// 当前对象属性
///
public System.IO.FileAttributes Attributes
{
get { return (System.IO.FileAttributes)myData.dwFileAttributes; }
}
///
/// 当前对象创建时间
///
public System.DateTime CreationTime
字串2
{
get
{
long time = ToLong(myData.ftCreationTime_dwHighDateTime, myData.ftCreationTime_dwLowDateTime);
System.DateTime dtm = System.DateTime.FromFileTimeUtc(time);
return dtm.ToLocalTime();
}
}
///
/// 当前对象最后访问时间
///
public System.DateTime LastAccessTime
{
get
{
long time = ToLong(myData.ftLastAccessTime_dwHighDateTime, myData.ftLastAccessTime_dwLowDateTime);
System.DateTime dtm = System.DateTime.FromFileTimeUtc(time);
return dtm.ToLocalTime();
}
}
///
/// 当前对象最后保存时间
///
字串3
public System.DateTime LastWriteTime
{
get
{
long time = ToLong(myData.ftLastWriteTime_dwHighDateTime, myData.ftLastWriteTime_dwLowDateTime);
System.DateTime dtm = System.DateTime.FromFileTimeUtc(time);
return dtm.ToLocalTime();
}
}
///
/// 当前文件长度,若为当前对象为文件则返回文件长度,若当前对象为目录则返回0
///
public long FileLength
{
get
{
if (this.bolIsFile)
return ToLong(myData.nFileSizeHigh, myData.nFileSizeLow);
else
return 0;
}
}
#endregion
Tags:
责任编辑:您的评论
·用户发表意见仅代表其个人意见,并且承担一切因发表内容引起的纠纷和责任
·本站管理人员有权在不通知用户的情况下删除不符合规定的评论信息或留做证据
·请客观的评价您所看到的资讯,提倡就事论事,杜绝漫骂和人身攻击等不文明行为
·本站管理人员有权在不通知用户的情况下删除不符合规定的评论信息或留做证据
·请客观的评价您所看到的资讯,提倡就事论事,杜绝漫骂和人身攻击等不文明行为
精彩推荐
最新资讯


您的位置: