Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / June 2006

Tip: Looking for answers? Try searching our database.

fast file count method?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rob R. Ainscough - 21 Jun 2006 17:24 GMT
Is there some way to determine file count on a drive?  I know I can use
System.IO.DriveInfo and My.Computer.FileSystem.GetFiles to obtain a file
count, but this approach is pretty slow -- is there a faster more efficient
method available?

.NET 2.0 framework is my base.

thanks, Rob.
Luca Beretta - 21 Jun 2006 20:46 GMT
the managed way is :

System.IO.DirectoryInfo dinfo = new System.IO.DirectoryInfo("c:\\windows");
int filecount = dinfo.GetFiles().Length;

otherwise you need to PInvoke C++ function from Win API.

HTH

> Is there some way to determine file count on a drive?  I know I can use
> System.IO.DriveInfo and My.Computer.FileSystem.GetFiles to obtain a file
[quoted text clipped - 4 lines]
>
> thanks, Rob.
Carl Daniel [VC++ MVP] - 22 Jun 2006 00:17 GMT
> the managed way is :
>
[quoted text clipped - 3 lines]
>
> otherwise you need to PInvoke C++ function from Win API.

... which will be just as slow as the method the OP suggested.

In short, no - there's no faster way to get a full count of files than by
counting them.  Exactly how you go about counting them is a matter of some
debate, but they'll all boil down to calls to FindFirstFile/FindNextFile at
the Win32 level and so will all take about the same amount of time.

You can use the class described in this article:

http://www.codeproject.com/cs/files/FileSystemEnumerator.asp

to count files as well - it will take the same amount of time as the methods
described above, but it won't take as much memory since it doesn't return a
string[] of file names.

-cd
Rob R. Ainscough - 22 Jun 2006 21:26 GMT
There has to be a more efficient approach?  Especially since windows index
services are running on most PCs.  Is the OS really that handicapped such
that it doesn't keep a pointer count?

>> the managed way is :
>>
[quoted text clipped - 20 lines]
>
> -cd
Damien - 23 Jun 2006 08:06 GMT
> There has to be a more efficient approach?  Especially since windows index
> services are running on most PCs.  Is the OS really that handicapped such
> that it doesn't keep a pointer count?
>
>From some of what you posted, I gather you're trying to count the
number of files on a disk, rather than just the number within a
particular directory?

If this is the case, then go into the root of your system drive,
highlight everything, and bring up the properties. Notice how the count
of files is goes up, as the system traverses all of the directories and
does file counts? If something as simple as the properties page is
having to do this work, it pretty much indicates to me that there isn't
a faster method available in Windows, or they'd have done it there
already.

Damien
Carl Daniel [VC++ MVP] - 23 Jun 2006 14:19 GMT
> There has to be a more efficient approach?  Especially since windows
> index services are running on most PCs.  Is the OS really that
> handicapped such that it doesn't keep a pointer count?

I don't know if I'd call it handicapped, but yes - there's no documented
better way.  The total file count simply isn't kept by the file system.  In
the metadata of NTFS there are counts of useful things (useful to the
filesystem) like the MFT size, and the number of used MFT records, but the
total file count is apparently not useful enough to be worth the effort of
maintaining it.

I suspect that a low-level scan of the MFT to count file records would be
more efficient, but there's no documented APIs for doing that.

-cd

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.