C#中获取文件名和最后一次写入时间大目录文件名、时间、目录

2023-09-04 22:35:35 作者:再辛苦也不说

使用C#(.NET 2.0),什么是检索文件由上次写入时间在网络目录列表,其中preference要么排序,或排除根据上次写入时间的回报,最好的方法是什么?

Using C# (.NET 2.0), what is the best way to retrieve a file listing in a network directory, with preference to either sort by last write time, or to exclude the return based on last write time?

目前我带回使用DirectoryInfo的实例GetFiles方法的列表。我遇到的目录可以包含超过6000个文件,只是为了得到FileInfo的数组后面需要近25秒。

I'm currently bringing back a listing using the GetFiles method of a DirectoryInfo instance. The directories I encounter can contain over 6,000 files and just to get an array of FileInfo back takes almost 25 seconds.

WMI将无法工作,我看不到任何东西是.NET为中心,将在这种情况下提供帮助。我失去了一些东西?有没有更好的解决方案,而不是使用的GetFiles?

WMI will not work and I cannot see anything else that is .NET-centric that will help in this situation. Am I missing something? Is there a better solution than to use GetFiles?

感谢你。

推荐答案

更新 我只注意到这个老回答,.NET 4.0和更高版本有一个 System.IO.EnumerateFiles 方法,该方法可以实现这个要求,一切pre .NET 4.0 ,请继续阅读。

Update I just noticed this old answer, for .net 4.0 and later there is a System.IO.EnumerateFiles method that does this for you, for everything pre .net 4.0, read on.

的.Net 3.5安培;此前

我和斯蒂芬,我遇到了这个确切的问题,并推出自己的枚举器遍历与+ 100K的文件的文件夹。

I'm with Stefan, I encountered this exact problem, and rolled my own enumerator to iterate over folders with +100k files.

本类包装所有你需要使用和用FindFirstFile的FindNextFile API的具体的东西。

This class wraps all the API specific stuff you'll need to use FindFirstFile and FindNextFile.

希望这有助于

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);
    }
}