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

  • SEARCH
  • Home
  • 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 172773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:13:31+00:00 2026-05-11T13:13:31+00:00

I am writing a directory scanner in .NET. For each File/Dir I need the

  • 0

I am writing a directory scanner in .NET.

For each File/Dir I need the following info.

   class Info {         public bool IsDirectory;         public string Path;         public DateTime ModifiedDate;         public DateTime CreatedDate;     } 

I have this function:

      static List<Info> RecursiveMovieFolderScan(string path){          var info = new List<Info>();         var dirInfo = new DirectoryInfo(path);         foreach (var dir in dirInfo.GetDirectories()) {             info.Add(new Info() {                 IsDirectory = true,                 CreatedDate = dir.CreationTimeUtc,                 ModifiedDate = dir.LastWriteTimeUtc,                 Path = dir.FullName             });              info.AddRange(RecursiveMovieFolderScan(dir.FullName));         }          foreach (var file in dirInfo.GetFiles()) {             info.Add(new Info()             {                 IsDirectory = false,                 CreatedDate = file.CreationTimeUtc,                 ModifiedDate = file.LastWriteTimeUtc,                 Path = file.FullName             });         }          return info;      } 

Turns out this implementation is quite slow. Is there any way to speed this up? I’m thinking of hand coding this with FindFirstFileW but would like to avoid that if there is a built in way that is faster.

  • 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. 2026-05-11T13:13:31+00:00Added an answer on May 11, 2026 at 1:13 pm

    This implementation, which needs a bit of tweaking is 5-10X faster.

        static List<Info> RecursiveScan2(string directory) {         IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);         WIN32_FIND_DATAW findData;         IntPtr findHandle = INVALID_HANDLE_VALUE;          var info = new List<Info>();         try {             findHandle = FindFirstFileW(directory + @'\*', out findData);             if (findHandle != INVALID_HANDLE_VALUE) {                  do {                     if (findData.cFileName == '.' || findData.cFileName == '..') continue;                      string fullpath = directory + (directory.EndsWith('\\') ? '' : '\\') + findData.cFileName;                      bool isDir = false;                      if ((findData.dwFileAttributes & FileAttributes.Directory) != 0) {                         isDir = true;                         info.AddRange(RecursiveScan2(fullpath));                     }                      info.Add(new Info()                     {                         CreatedDate = findData.ftCreationTime.ToDateTime(),                         ModifiedDate = findData.ftLastWriteTime.ToDateTime(),                         IsDirectory = isDir,                         Path = fullpath                     });                 }                 while (FindNextFile(findHandle, out findData));              }         } finally {             if (findHandle != INVALID_HANDLE_VALUE) FindClose(findHandle);         }         return info;     } 

    extension method:

     public static class FILETIMEExtensions {         public static DateTime ToDateTime(this System.Runtime.InteropServices.ComTypes.FILETIME filetime ) {             long highBits = filetime.dwHighDateTime;             highBits = highBits << 32;             return DateTime.FromFileTimeUtc(highBits + (long)filetime.dwLowDateTime);         }     } 

    interop defs are:

        [DllImport('kernel32.dll', CharSet = CharSet.Unicode, SetLastError = true)]     public static extern IntPtr FindFirstFileW(string lpFileName, out WIN32_FIND_DATAW lpFindFileData);      [DllImport('kernel32.dll', CharSet = CharSet.Unicode)]     public static extern bool FindNextFile(IntPtr hFindFile, out WIN32_FIND_DATAW lpFindFileData);      [DllImport('kernel32.dll')]     public static extern bool FindClose(IntPtr hFindFile);      [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]     public struct WIN32_FIND_DATAW {         public FileAttributes dwFileAttributes;         internal System.Runtime.InteropServices.ComTypes.FILETIME ftCreationTime;         internal System.Runtime.InteropServices.ComTypes.FILETIME ftLastAccessTime;         internal System.Runtime.InteropServices.ComTypes.FILETIME ftLastWriteTime;         public int nFileSizeHigh;         public int nFileSizeLow;         public int dwReserved0;         public int dwReserved1;         [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]         public string cFileName;         [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]         public string cAlternateFileName;     } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.