如题
public static bool setDNS(string dns) { bool result = true; ManagementClass mcNetworkAdapter = new ManagementClass("Win32_NetworkAdapter"); ManagementClass mcNetworkConfiguration = new ManagementClass("Win32_NetworkAdapterConfiguration"); foreach (ManagementObject mo in mcNetworkConfiguration.GetInstances()) { if ((bool)mo["IPEnabled"] == true) { //mo.InvokeMethod("SetDNSServerSearchOrder", new string[]{dns}); mo.InvokeMethod("SetDNSServerSearchOrder",new Object[]{dns,""}); foreach (ManagementObject mo2 in mcNetworkAdapter.GetInstances()) { if ((int)mo2["index"] == (int)mo["index"]) { mo2.InvokeMethod("Disable", new object[] { }); mo2.InvokeMethod("Enable", new object[] { }); break; } } } } return result; }
执行到这一句的时候 mo.InvokeMethod("SetDNSServerSearchOrder",new Object[]{dns,""})
出现[无法将类型为“System.String”的对象强制转换为类型“System.Array""异常……
无解中……
看下这个
InvokeMethod要求的参数
SetDNSServerSearchOrder 方法需要一个数组形式的dns,而你传入的是一个字符串的,所以出错,你可以将这个字符串分割下