C#获取大目录的文件名和上次写入时间
使用C#(.NET 2.0),检索网络目录中的文件列表的最佳方法是什么,首选按上次写入时间排序,或根据上次写入时间排除返回 我目前正在使用DirectoryInfo实例的GetFiles方法返回一个列表。我遇到的目录可以包含6000多个文件,仅获取一个FileInfo数组几乎需要25秒 WMI将不起作用,我看不到任何其他以.NET为中心的在这种情况下有帮助的东西。我错过什么了吗?有比使用GetFiles更好的解决方案吗C#获取大目录的文件名和上次写入时间,c#,.net,file,C#,.net,File,使用C#(.NET 2.0),检索网络目录中的文件列表的最佳方法是什么,首选按上次写入时间排序,或根据上次写入时间排除返回 我目前正在使用DirectoryInfo实例的GetFiles方法返回一个列表。我遇到的目录可以包含6000多个文件,仅获取一个FileInfo数组几乎需要25秒 WMI将不起作用,我看不到任何其他以.NET为中心的在这种情况下有帮助的东西。我错过什么了吗?有比使用GetFiles更好的解决方案吗 谢谢。问题不是FileInfo实例的排序规则,而是访问上次写入时间时执行的A
谢谢。问题不是FileInfo实例的排序规则,而是访问上次写入时间时执行的API调用
要获得更快的响应,您必须自己调用Win32 API函数FindFirstFile和FindNextFile。更新
我刚刚注意到这个老答案,对于.NET4.0和更高版本,有一个
System.IO.EnumerateFiles
方法可以为您实现这一点,对于.NET4.0之前的所有内容,请继续阅读
.Net 3.5及更早版本
我和Stefan在一起,我遇到了这个确切的问题,并使用我自己的枚举器对+100k文件的文件夹进行迭代
此类封装了使用FindFirstFile和FindNextFile所需的所有特定于API的内容
希望这有帮助
internal class APIWrapper
{
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
internal sealed class FILETIME
{
public int Low;
public int High;
public Int64 ToInt64()
{
Int64 h = High;
h = h << 32;
return h + Low;
}
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
internal sealed class FindData
{
public int fileAttributes;
public FILETIME CreationTime;
public FILETIME LastAccessTime;
public FILETIME LastWriteTime;
public int FileSizeHigh;
public int FileSizeLow;
public int dwReserved0;
public int dwReserved1;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public String fileName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
public String alternateFileName;
}
internal sealed class SafeFindHandle : Microsoft.Win32.SafeHandles.SafeHandleMinusOneIsInvalid
{
/// <summary>
/// Constructor
/// </summary>
public SafeFindHandle()
: base(true)
{
}
/// <summary>
/// Release the find handle
/// </summary>
/// <returns>true if the handle was released</returns>
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
protected override bool ReleaseHandle()
{
return SafeNativeMethods.FindClose(handle);
}
}
internal enum SearchOptions
{
NameMatch,
LimitToDirectories,
LimitToDevices
}
[SecurityPermissionAttribute(SecurityAction.Assert, UnmanagedCode = true)]
internal static class SafeNativeMethods
{
[DllImport("Kernel32.dll", CharSet = CharSet.Auto)]
public static extern SafeFindHandle FindFirstFile(String fileName, [In, Out] FindData findFileData);
[DllImport("Kernel32.dll", CharSet = CharSet.Auto)]
public static extern SafeFindHandle FindFirstFileEx(
String fileName, //__in LPCTSTR lpFileName,
[In] int infoLevel, //__in FINDEX_INFO_LEVELS fInfoLevelId,
[In, Out] FindData findFileData, //__out LPVOID lpFindFileData,
[In, Out] SearchOptions SerchOps, //__in FINDEX_SEARCH_OPS fSearchOp,
[In] int SearchFilter, //__reserved LPVOID lpSearchFilter,
[In] int AdditionalFlags); //__in DWORD dwAdditionalFlags
[DllImport("kernel32", CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool FindNextFile(SafeFindHandle hFindFile, [In, Out] FindData lpFindFileData);
[DllImport("kernel32", CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool FindClose(IntPtr hFindFile);
}
}
内部类APIWrapper
{
[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Auto)]
内部密封类文件时间
{
公众智力低下;
公众智力高;
公共Int64到Int64()
{
Int64 h=高;
我刚刚意识到我没有投票支持你的答案,我早就该这么做了。