WMI 类求助
我想用WMI类来改电脑的IP,代码如下:
private static void setIPAddress(string[] ip, string[] submask, string[] getway, string[] dns)
{
ManagementClass wmi = new ManagementClass("Win32_NetworkAdapterConfiguration");
using (ManagementObjectCollection moc = wmi.GetInstances())
{
ManagementBaseObject inPar = null;
ManagementBaseObject outPar = null;
foreach (ManagementObject mo in moc)
{
//如果没有启用IP设置的网络设备则跳过
if (!(bool)mo["IPEnabled"])
continue;
//设置IP地址和掩码
if (ip != null && submask != null)
{
inPar = mo.GetMethodParameters("EnableStatic");
inPar["IPAddress"] = ip;
inPar["SubnetMask"] = submask;
outPar = mo.InvokeMethod("EnableStatic", inPar, null);
object x=outPar.GetPropertyValue("ReturnValue");
}
//设置网关地址
if (getway != null)
{
inPar = mo.GetMethodParameters("SetGateways");
inPar["DefaultIPGateway"] = getway;
outPar = mo.InvokeMethod("SetGateways", inPar, null);
}
//设置DNS地址
if (dns != null)
{
inPar = mo.GetMethodParameters("SetDNSServerSearchOrder");
inPar["DNSServerSearchOrder"] = dns;
outPar = mo.InvokeMethod("SetDNSServerSearchOrder", inPar, null);
}
}
}
}
现在的问题是,运行之后没有报错但是我机器的IP也没有变化。于是我就想看看outPar的值,然后发现单步运行的时候outPar的值根本看不见,VS会说计算超时什么的。
于是我用object x=outPar.GetPropertyValue("ReturnValue");去取值,发现会得到一个非常大的整数,与MSDN上面的ReturnCode毫不相关。
请教高人,我的程序有什么问题,怎么改呢?
WMI
[解决办法]
http://blog.csdn.net/liushuijinger/article/details/8589254
这个参考一下,看有用没?