WMI 搜索通配符不返回任何内容

发布于 2024-12-19 19:41:42 字数 1133 浏览 2 评论 0原文

编辑:添加了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文