Large directories... DirectoryInfo or Win32 API?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Mike G. (MikeG_at_discussions.microsoft.com)
Date: 09/29/04


Date: Wed, 29 Sep 2004 06:51:01 -0700

Hi All,

I have a situation where I have several directories each containing hundreds
of thousands of files. I have written a program which looks at each file in a
directory, moves it to a subdirectory of a more manageable size, and updates
the file location in our database. I initially used DirectoryInfo.GetFiles()
to populate a FileInfo array to loop through. This turned out to be so slow
on these large directories that files were being added there faster than they
could be moved. It appears that when GetFiles is called it loads the entire
directory into memory before proceding to the next line.

To solve this problem I put the Win32 API functions FindFirstFile,
FindNextFile, and FindClose in a wrapper class and created a struct to hold
the FileData. This works much faster and is an acceptable solution. However,
I would like to know, is there a way of doing this natively in .NET without
directly accessing the Win32 API? I would really like to stay in the world of
managed code if possible.

Thanks,

Mike G.



Relevant Pages

  • Re: Large directories... DirectoryInfo or Win32 API?
    ... Blog: http://weblogs.asp.net/justin_rogers "Justin Rogers" wrote in message ... >> To solve this problem I put the Win32 API functions FindFirstFile,>> FindNextFile, and FindClose in a wrapper class and created a struct to hold>> the FileData. ... I would really like to stay in the world of>> managed code if possible. ... >> Mike G.> ...
    (microsoft.public.dotnet.framework.performance)
  • RE: Managed OS?
    ... As far as I can understand, Longhorn will be built using Win32 API. ... Windows OS functionality is vast and some of it could be accessed using ... that will be built with managed code. ...
    (Security-Basics)
  • Re: textbox and size.
    ... the Win32 API is always available, and I use it occasionally for tasks that ... aren't available through Managed code. ... I had to add cludges to multiply heights by 1.05, etc, and still often ...
    (microsoft.public.dotnet.general)
  • Re: Large directories... DirectoryInfo or Win32 API?
    ... Internally they are doing the Win32 API calls for you. ... also allocating a LOT of array space to do it. ... I would really like to stay in the world of> managed code if possible. ... > Mike G. ...
    (microsoft.public.dotnet.framework.performance)
  • Re: What is managed code?
    ... managed code is code that the runtime system handles all ... > aspects of memory and resource management. ... But since we know that resources on any given ... > also reference counted, which means that it is possible to create two ...
    (microsoft.public.vb.general.discussion)