daocaoren_lb 发表于 2017-8-18 19:23:53

C#程序修改IP地址哪里出错了

程序运行时并没有报错,可就是修改没反应,哪位指点一下
static void Main(string[] args)
      {
            ManagementClass wmi = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = wmi.GetInstances();
            ManagementBaseObject inPar = null;
            ManagementBaseObject outPar = null;
            string[] ip = { "192.168.1.3", "192.168.1.4" };
            string[] submask = { "255.255.255.0"};
            string[] gatway = { "192.168.1.2", "192.168.1.1" };
            string[] dns = { "114.114.114.114", "114.114.114.114" };
            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);
                }
                //设置网关地址
                if (gatway != null)
                {
                  inPar = mo.GetMethodParameters("SetGateways");
                  inPar["DefaultIPGateway"] = gatway;
                  outPar = mo.InvokeMethod("SetGateways", inPar, null);
                }
                //设置DNS地址
                if (dns != null)
                {
                  inPar = mo.GetMethodParameters("SetDNSServerSearchOrder");
                  inPar["DNSServerSearchOrder"] = dns;
                  outPar = mo.InvokeMethod("SetDNSServerSearchOrder", inPar, null);
                }
            }
      }

页: [1]
查看完整版本: C#程序修改IP地址哪里出错了