在管理员模式下无法查询 WMI

Cant query WMI when in Administrator mode

我花了半天时间才意识到,每当程序 (c#) 在管理员模式下 运行ning 时,我对 WMI 的所有查询都以计数 0 返回,但是只要它是 运行 通常它会收到 7 的计数(这是我应该得到的正确计数值)及其相关值。如果 VS 在管理员模式下 运行ning,在调试模式下也会出现此问题。

这是 windows 10 最终版(并于 2015 年 7 月 31 日完成更新)。尝试了不同的框架 (4.0->4.6) 以防与此有关,但显然没有。

这是在正常情况下工作的代码,但在 ADMIN 模式下不工作:

public static Dictionary<String, String> GetNetworkDrives()
        {
            Dictionary<String, String> RetuningDic = new Dictionary<string, string>();
            try
            {
                ManagementObjectSearcher searcher =
                    new ManagementObjectSearcher("root\CIMV2",
                        "SELECT * FROM Win32_MappedLogicalDisk");
                MessageBox.Show("Query count: " + searcher.Get().Count.ToString());
                foreach (ManagementObject queryObj in searcher.Get())
                {
                    //Console.WriteLine("-----------------------------------");
                    //Console.WriteLine("Win32_MappedLogicalDisk instance");
                    //Console.WriteLine("-----------------------------------");
                    MessageBox.Show(queryObj["VolumeName"] + "||" + queryObj["DeviceID"]);
                    //String stuff = queryObj["ProviderName"] + "||" + queryObj["DeviceID"]; //queryObj["VolumeName"]
                    //Console.WriteLine(stuff);
                }
                MessageBox.Show("No errors");
            }
            catch (ManagementException e)
            {
                //Console.WriteLine("Error");
                MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
            }
            return RetuningDic;
        }

代码说明:Return 消息框上的字母和映射网络位置的 folder/volume 名称。

Win32_MappedLogicalDisk的备注,特别是:

However, whether instances are returned depends on the Local Security Policy settings in the Control Panel Administrative Tools. If the following policy is set to "Object Creator", then no mapped drive instances are returned, even if A is a member of the Administrators group: "System object: default owner for objects created by members of the administrators group."

If there is more than one session for user A on the machine, then no mapped drive instances are returned (because the provider has no reasonable way of deciding which session to use).

所以有些情况下 Win32_MappedLogicalDisk 没有 return 驱动器,即使它们被添加。我可能会检查安全设置、策略等。