我如何找出多少个文件所在的目录?多少个、所在、文件、目录

2023-09-03 00:50:26 作者:′战斗法师┒

我需要得到的文件数的计数中的一个目录。我能得到在目录中使用的所有文件的名称 System.IO.Directory.GetFiles(),并采取数组的长度,但时间过长的大目录。有没有办法让刚刚计数,而无需得到的名字?

I need to get a count of the number of files in a directory. I could get the names of all the files in the directory using System.IO.Directory.GetFiles() and take the length of that array but that takes too long on large directories. Is there a way to get just the count without having to get the names?

推荐答案

我不这么认为,没有 - 至少在香草.NET。我怀疑这是不是花时间的名称的实际抓取 - 这是通过操作系统的目录内部走。这里的可以的是一个Win32叫你可以通过P / Invoke的制作。

I don't believe so, no - at least not in vanilla .NET. I suspect it's not the actual fetching of the names that takes the time - it's the OS walking through the directory internals. There may be a Win32 call you could make via P/Invoke.

多大的目录你在看什么?一般来说它至少的传统的没有有超过一个目录中几百个文件是一个好主意。文件系统已在这个情况的改善,但我不知道目前的状态与NTFS和FAT32的东西。

How large is the directory you're looking at? In general it's at least traditionally not been a good idea to have more than a few hundred files in a directory. File systems have improved at this in general, but I don't know what the current state is with NTFS and Fat32.