Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

*FASTEST* directory listing

I have massive directories, and I would like to read all the files as fast as I can. I mean, not DirectoryInfo.GetFiles fast, but 'get-clusters-from-disk-low-level' fast.

Of course, .NET 2.0, c#

Similar question was here, but this approach wasn't any good:

C# Directory listing massive directory

Someone suggested pInvoke on FindFirst/FindNext. Anybody tried that and is able to share results?

like image 683
Daniel Mošmondor Avatar asked Aug 27 '10 09:08

Daniel Mošmondor


People also ask

How can I get a list of all directories?

The ls command is used to list files or directories in Linux and other Unix-based operating systems. Just like you navigate in your File explorer or Finder with a GUI, the ls command allows you to list all files or directories in the current directory by default, and further interact with them via the command line.

What is the top most directory?

In a computer file system, and primarily used in the Unix and Unix-like operating systems, the root directory is the first or top-most directory in a hierarchy. It can be likened to the trunk of a tree, as the starting point where all branches originate from.

What is a directory * 1 point?

A directory is a location for storing files on your computer. Directories are found in a hierarchical file system, such as Linux, MS-DOS, OS/2, and Unix. Pictured is an example of output from the Windows/DOS tree command. It shows all the local and subdirectories (e.g., the "big" directory in the "cdn" directory).

Which command is used for listing directories?

Use the ls command to display the contents of a directory. The ls command writes to standard output the contents of each specified Directory or the name of each specified File, along with any other information you ask for with the flags.


2 Answers

For a "normal" approach, basically everything boils down to FindFirstFile/FindNextFile, you don't really get much faster than that... and that isn't super-turbo-fast.

If you really need speed, look into reading the MFT manually - but know that this requires admin privileges, and is prone to break whenever NTFS gets updated (and, oh yeah, won't work for non-NTFS filesystems). You might want to have a look at this code which has USN and MFT stuff.

However, perhaps there's a different solution. If your app is running constantly and needs to pick up changes, you can start off by doing one slow FindFirstFile/FindNextFile pass, and then use directory change notification support to be informed of updates... that works for limited users, and doesn't depend on filesystem structures.

like image 187
snemarch Avatar answered Oct 05 '22 23:10

snemarch


For the best performance, it is possible to P/Invoke NtQueryDirectoryFile, documented as ZwQueryDirectoryFile.

(That short of accessing the disk directly and reading the raw file system structures directly, which usually is not practical.)

like image 20
nick.lowe Avatar answered Oct 05 '22 23:10

nick.lowe