*最快*目录列表
我有大量目录,我想尽快读取所有文件。我的意思是,不是 DirectoryInfo.GetFiles 快,而是“从磁盘低级获取集群”快。
当然,.NET 2.0, c#
类似的问题在这里,但这种方法没有任何好处:
有人建议在 FindFirst/FindNext 上使用 pInvoke。有人尝试过并能够分享结果吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于“正常”方法,基本上一切都归结为
FindFirstFile
/FindNextFile
,您实际上不会比这更快......而且这不是超级 -涡轮快速。如果您确实需要速度,请考虑手动阅读MFT - 但是知道这需要管理员权限,并且每当 NTFS 更新时很容易被破坏(而且,哦,是的,不适用于非 NTFS 文件系统)。您可能想看看这段代码,其中有< a href="http://en.wikipedia.org/wiki/USN_Journal" rel="noreferrer">USN 和 MFT 内容。
然而,也许有不同的解决方案。如果您的应用持续运行并且需要获取更改,您可以通过执行一次缓慢的
FindFirstFile
/FindNextFile
传递开始,然后使用目录更改通知支持来获取通知更新...适用于有限的用户,并且不依赖于文件系统结构。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.为了获得最佳性能,可以 P/Invoke NtQueryDirectoryFile,记录为 ZwQueryDirectoryFile。
(直接访问磁盘并直接读取原始文件系统结构的缺点,这通常是不切实际的。)
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.)
尝试使用此 DirectoryManager 之类的东西,并根据您的需求进行优化。比 .NET Framework
GetDirectories()
或GetFiles()
工作得更快,因为我们省略了跨平台检查和调整。Try using something like this DirectoryManager and refine it by your needs. Works faster than the .NET Framework
GetDirectories()
orGetFiles()
because we ommitted there cross-platform checkings and adaptations.