shell - 通配符和 IShellFolder 枚举?
问题描述
我想尝试远离FindFirstFile()
/FindNextFile()
并直接使用IShellFolder
以获取文件列表。原因是我想为每个文件获取一个IShellItem
通过SHNCreateItemFromIDList()
,并且我将拥有传递给函数所需的一切。如果我使用文件路径函数,我想我每次都需要在传递到之前构建完整路径SHCreateItemFromParsingName()
,但我会问另一个特定的问题。
我的问题仅限于IShellFolder
使用通配符枚举文件和文件夹。有内置的东西可以做到这一点,还是你必须自己做文件匹配?
蒂亚!!
解决方案
您不能使用 IShellFolder 进行过滤,但可以使用 Shell 中内置的搜索工具以编程方式执行相同的操作,就像使用 Windows 资源管理器 UI 一样。
例如,您可以在右上角的搜索框中键入类似ext:.txt的内容,这意味着您要过滤所有具有.txt扩展名的文件:
这是一些等效的 C++ 示例代码(我已经删除了每一行的错误检查,但请确保您测试了所有可能的错误):
int main()
{
CoInitialize(NULL);
{
CComPtr<ISearchFolderItemFactory> search;
CComPtr<IShellItem> item;
CComPtr<IShellItemArray> items;
CComPtr<IQueryParserManager> mgr;
CComPtr<IQueryParser> parser;
CComPtr<IQuerySolution> solution;
CComPtr<ICondition> condition;
CComPtr<IShellItem> searchItem;
CComPtr<IEnumShellItems> enumItems;
// create search folder factory
search.CoCreateInstance(CLSID_SearchFolderItemFactory);
// create d:\temp shell item and set search folder scope to it
SHCreateItemFromParsingName(L"d:\\temp", NULL, IID_PPV_ARGS(&item));
SHCreateShellItemArrayFromShellItem(item, IID_PPV_ARGS(&items));
search->SetScope(items);
// create the query parser manager
mgr.CoCreateInstance(CLSID_QueryParserManager);
mgr->CreateLoadedParser(L"", 0, IID_PPV_ARGS(&parser));
// parse an ms-search expression
parser->Parse(L"ext:.txt", NULL, &solution);
// get the condition the parser has built for us
solution->GetQuery(&condition, NULL);
// give the condition to the search folder factory
search->SetCondition(condition);
// get the search result back as a shell item (a virtual folder) and enumerates it
search->GetShellItem(IID_PPV_ARGS(&searchItem));
searchItem->BindToHandler(NULL, BHID_EnumItems, IID_PPV_ARGS(&enumItems));
do
{
CComPtr<IShellItem> child;
ULONG fetched;
HRESULT hr2 = enumItems->Next(1, &child, &fetched);
if (!fetched)
break;
// get the display name (for example)
CComHeapPtr<WCHAR> name;
child->GetDisplayName(SIGDN_NORMALDISPLAY, &name);
wprintf(L"item: %s\n", name);
CComHeapPtr<WCHAR> path;
child->GetDisplayName(SIGDN_FILESYSPATH, &path);
wprintf(L" path: %s\n", path);
} while (TRUE);
}
CoUninitialize();
return 0;
}
search-ms语言非常强大。它的语法在这里可用:Querying the Index with the search-ms Protocol
推荐阅读
- jquery - 弄清楚最后一步 Jquery Smart Wizard 5?
- facebook - 在 Spark AR 中实施材料时遇到问题
- sql - 如何从 N1QL 查询中的嵌套数组中提取最大值和最小值?
- java - Java 微流列表对象未正确存储
- laravel - 如何在表单请求验证中使用忽略规则
- redis - 如何使用 Kafka Streams 将数据写入 Redis 自定义状态存储
- python - 面板数据拆分
- android - React Native Share 间歇性地无法在 android 上运行
- c# - C# 无法转换对象
- here-api - Here Fleet Telematics 管理区域