WMI 搜索通配符不返回任何内容
编辑:添加了 oScope
当我运行不带通配符的查询时,它会返回我期望的结果。但如果我稍微修改一下查询,它就不会抛出错误。相反,它只是不返回任何内容,给我一个假阴性。
下面的代码返回零行:
ManagementScope oScope = new ManagementScope("\\\\" + txtHostName +
"\\root\\CIMV2", oConn);
string wheresub = "FileName Like '%myfile%'";
ObjectQuery oQuery = new ObjectQuery(@"SELECT * FROM CIM_DataFile WHERE (" +
wheresub + ") AND (Drive = 'C:' OR Drive = 'D:' OR Drive = 'E:')");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(oScope, oQuery);
Log("Searching...");
foreach (ManagementObject queryObj in searcher.Get())
{
FilesFound++;
Log(String.Format("--> {0}{1}{2}.{3}", queryObj["Drive"], queryObj["Path"], queryObj["FileName"], queryObj["Extension"]));
}
if (FilesFound > 0)
{
Log(" " + FilesFound + " files found.");
}
else
{
Log(" Clean.");
}
这段代码始终返回“Clean”。但是,如果我更改一行来查找特定文件,它会找到一个特定文件:
string wheresub = "FileName = 'PROCID_myfile_1234'";
我需要通配符版本,以便我可以找到所有可能的文件,而不仅仅是一个特定文件。我做错了什么?
EDIT: Added oScope
When I run my query without wildcards, it returns what I expect it to. But if I modify the query just a little, it doesn't throw errors. Instead, it just doesn't return anything, giving me a false-negative.
The code below returns ZERO rows:
ManagementScope oScope = new ManagementScope("\\\\" + txtHostName +
"\\root\\CIMV2", oConn);
string wheresub = "FileName Like '%myfile%'";
ObjectQuery oQuery = new ObjectQuery(@"SELECT * FROM CIM_DataFile WHERE (" +
wheresub + ") AND (Drive = 'C:' OR Drive = 'D:' OR Drive = 'E:')");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(oScope, oQuery);
Log("Searching...");
foreach (ManagementObject queryObj in searcher.Get())
{
FilesFound++;
Log(String.Format("--> {0}{1}{2}.{3}", queryObj["Drive"], queryObj["Path"], queryObj["FileName"], queryObj["Extension"]));
}
if (FilesFound > 0)
{
Log(" " + FilesFound + " files found.");
}
else
{
Log(" Clean.");
}
This slice of code always returns "Clean." But if I change ONE line, to find a specific file, it finds the one specific file:
string wheresub = "FileName = 'PROCID_myfile_1234'";
I need the wildcard version so that I can find all of the possible files, not just a specific one. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论