简体   繁体   English

使用C ++比使用FindFirstFile / FindNextFile枚举文件夹有更快的替代方法吗?

[英]Is there a faster alternative to enumerating folders than FindFirstFile/FindNextFile with C++?

I need to get all paths to subfolders within a folder (with WinAPIs and C++.) So far the only solution that I found is recursively calling FindFirstFile / FindNextFile but it takes a significant amount of time to do this on a folder with a deeper hierarchy. 我需要获取文件夹中的子文件夹的所有路径(使用WinAPI和C ++。)到目前为止,我找到的唯一解决方案是递归调用FindFirstFile / FindNextFile,但是在具有更深层次结构的文件夹上执行此操作需要花费大量时间。

So I was wondering, just to get folder names, is there a faster approach? 所以我想知道,只是为了得到文件夹名称,有更快的方法吗?

If you really just need subfolders you should be able to use FindFirstFileEx with search options to filter out non-directories. 如果你真的只需要子文件夹,你应该能够使用FindFirstFileEx搜索选项来过滤掉非目录。

The docs suggest this is an advisory flag only, but your filesystem may support this optimization - give it a try. 文档建议这只是一个咨询标志,但您的文件系统可能支持此优化 - 尝试一下。

FindExSearchLimitToDirectories FindExSearchLimitToDirectories

This is an advisory flag. 这是一个咨询标志。 If the file system supports directory filtering, the function searches for a file that matches the specified name and is also a directory. 如果文件系统支持目录过滤,则该函数将搜索与指定名称匹配的文件,并且也是目录。 If the file system does not support directory filtering, this flag is silently ignored. 如果文件系统不支持目录过滤,则会以静默方式忽略此标志。

That is the fastest approach you can come across. 这是您遇到的最快的方法。 Also you may consider using another thread to manage directory enumerations as it takes a lot of time. 您也可以考虑使用另一个线程来管理目录枚举,因为它需要花费很多时间。 even Microsoft file explorer spend some time if the directory has a lot of sub folders/files. 如果目录中有很多子文件夹/文件,即使是Microsoft文件浏览器也要花一些时间。

One more thing here is that you can enumerate directories once and then register for any updates. 这里还有一件事是你可以枚举一次目录,然后注册任何更新。 so the cost of enumerating the folder should be made only once during start up. 所以枚举文件夹的成本应该只在启动时进行一次。

A faster approach would be to bypass the FindFirstFile...() API and go straight to the file system directly. 更快的方法是绕过FindFirstFile...() API并直接直接进入文件系统。 You can use DeviceIoControl() with the FSCTL_ENUM_USN_DATA control to access the master file table, at least on NTFS formatted volumes. 您可以将DeviceIoControl()FSCTL_ENUM_USN_DATA控件一起使用来访问主文件表,至少在NTFS格式化的卷上。 With that information, you can directly access the records for files/folders, which includes their attributes, parent info, etc. Yes, it would be more work, but it should also be faster since you can optimize the code to access just the pieces you need. 有了这些信息,您就可以直接访问文件/文件夹的记录,其中包括它们的属性,父信息等。是的,它会更有效,但它也应该更快,因为您可以优化代码来访问这些部分你需要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM