【问题标题】:Searching for a File on Remote Machine WMI C#在远程计算机 WMI C# 上搜索文件
【发布时间】:2012-10-23 03:07:31
【问题描述】:

我想在远程机器上搜索一个文件。我不知道确切的文件路径,但我知道它在 C:\Windows\System

我的查询在 WMI 中是这样的

string querystr = "SELECT * FROM CIM_DataFile Where Path='C:\\Windows\\System'";
ObjectQuery query = new ObjectQuery(querystr );
ManagementObjectSearcher Searcher = new ManagementObjectSearcher(Scope, query);

我收到无效查询错误。

查询有效吗?有什么方法可以指定路径下?

【问题讨论】:

    标签: c# wmi wmi-query


    【解决方案1】:

    您的代码中有两个问题

    1. 您必须对 \ 字符进行双重转义,因为这是 WMI 中的保留符号
    2. 路径属性不得包含驱动器。

    试试这个示例

    using System;
    using System.Collections.Generic;
    using System.Management;
    using System.Text;
    
    namespace GetWMI_Info
    {
        class Program
        {
    
            static void Main(string[] args)
            {
                try
                {
                    string ComputerName = "localhost";
                    ManagementScope Scope;                
    
                    if (!ComputerName.Equals("localhost", StringComparison.OrdinalIgnoreCase)) 
                    {
                        ConnectionOptions Conn = new ConnectionOptions();
                        Conn.Username  = "";
                        Conn.Password  = "";
                        Conn.Authority = "ntlmdomain:DOMAIN";
                        Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", ComputerName), Conn);
                    }
                    else
                        Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", ComputerName), null);
    
                    Scope.Connect();
                    string Drive = "c:";
                    //look how the \ char is escaped. 
                    string Path = "\\\\Windows\\\\System32\\\\";
                    ObjectQuery Query = new ObjectQuery(string.Format("SELECT * FROM CIM_DataFile Where Drive='{0}' AND Path='{1}' ", Drive, Path));
    
                    ManagementObjectSearcher Searcher = new ManagementObjectSearcher(Scope, Query);
    
                    foreach (ManagementObject WmiObject in Searcher.Get())
                    {
                        Console.WriteLine("{0}",(string)WmiObject["Name"]);// String
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(String.Format("Exception {0} Trace {1}",e.Message,e.StackTrace));
                }
                Console.WriteLine("Press Enter to exit");
                Console.Read();
            }
        }
    }
    

    【讨论】:

    • 如果文件位于 C:\Windows\System32\Drivers 下,这将不起作用。它只在 C:\Windows\System32 下查找。如何指定查看该路径下的所有文件夹?
    • 您可以使用LIKE 运算符并以这种方式重写WQL 语句SELECT * FROM CIM_DataFile Where Drive='{0}' AND Path LIKE '%{1}%',但这将花费很多时间,因为WMI 将扫描整个驱动器以查找匹配项。我认为最好的方法是使用CIM_DataFileWin32_Directory 类构造递归函数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-20
    • 2012-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多