I have some directories containing test data, typically over 200,000 small (~4k) files per directory.
I am using the following C# code to get the number of files in a di
The code you've got is slow because it first gets an array of all the available files, then takes the length of that array.
However, you're almost certainly not going to find any solutions that work much faster than that.
Why?
Access controls.
Each file in a directory may have an access control list - which may prevent you from seeing the file at all.
The operating system itself can't just say "hey, there are 100 file entries here" because some of them may represent files you're not allowed to know exist - they shouldn't be shown to you at all. So the OS itself has to iterate over the files, checking access permissions file by file.
For a discussion that goes into more detail around this kind of thing, see two posts from The Old New Thing:
[As an aside, if you want to improve performance of a directory containing a lot of files, limit yourself to strictly 8.3 filenames. No I'm not kidding - it's faster, because the OS doesn't have to generate an 8.3 filename itself, and because the algorithm used is braindead. Try a benchmark and you'll see.]