Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 459517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:46:51+00:00 2026-05-12T22:46:51+00:00

Using C# (.NET 2.0), what is the best way to retrieve a file listing

  • 0

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?

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 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?

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T22:46:51+00:00Added an answer on May 12, 2026 at 10:46 pm

    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 & Earlier

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

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

    Hope this helps,

    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);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 315k
  • Answers 315k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You could use the views_groupby module to do this. It's… May 13, 2026 at 11:11 pm
  • Editorial Team
    Editorial Team added an answer Not much, but here's a little to get you started:… May 13, 2026 at 11:11 pm
  • Editorial Team
    Editorial Team added an answer Luca Bolognese posted this in his blog. Here's the relevant… May 13, 2026 at 11:11 pm

Related Questions

What is the best way to embed a truetype font within the application i'm
This is all in C#, using .NET 2.0. I have two lists of objects.
There is the first time I'm going to do a particular control in C#.
I am trying to optimize a piece of .NET 2.0 C# code that looks

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.