通过WMI获取Networkadatper配置时出现“无效的查询类型”错误

时间:2013-04-15 07:10:32

标签: c# wmi

我试图通过传递主机名来获取netpadtapter配置,例如ipaddress,IPSubnet,WINSPrimaryServer,WINSSecondaryServer。

但它会引发以下错误

System.Management.ManagementStatus.InvalidQueryType

以下是我使用的代码

query = new ObjectQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE DNSHostName='{0}'", objServerInfo.DNSHostName);
                    searcher = new ManagementObjectSearcher(mgmtScope, query);
                    ManagementObjectCollection networkConfigresults = searcher.Get();
                    foreach (ManagementObject queryObj in networkConfigresults)
                    {
                        NetworkConfig objNetworkConfig = new NetworkConfig();
                        objNetworkConfig.serverId = sysconfig.SystemId;
                        objNetworkConfig.IPAdresss =  queryObj["IPAddress"] != null ? String.Join(",", (string[])queryObj["IPAddress"]) : string.Empty;
                        objNetworkConfig.subnetMask = queryObj["IPSubnet"] != null ? String.Join(",", (string[])queryObj["IPSubnet"]) : string.Empty;
                        objNetworkConfig.primaryDNSServer = Convert.ToString(queryObj["WINSPrimaryServer"]);
                        objNetworkConfig.alternateDNSServer = Convert.ToString(queryObj["WINSSecondaryServer"]);
                        lstNetworkConfig.Add(objNetworkConfig);
                    }

请帮我解决这个问题

1 个答案:

答案 0 :(得分:2)

按如下方式更改您的查询

query = new ObjectQuery(String.Format("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE DNSHostName='{0}'", objServerInfo.DNSHostName));