日期:2014-05-17  浏览次数:21106 次

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");