C#网络配置更改无法正常工作

时间:2016-07-07 03:39:49

标签: c# configuration system

    public class NetworkConfig
    {
        public enum NetworkType
        {
            Static,
            Dynamic
        }

        public bool SetNetworkConfiguration(string adaptName, string cIPAddress, string cSubnetMask ,string cGateway, string[] cDNS, NetworkType nType)
        {
            var mClass = new ManagementClass("Win32_NetworkAdapterConfiguration");
            var nCol = mClass.GetInstances();

            foreach(ManagementObject mObject in nCol)
            {
                string searchAdapt = (string)mObject["Description"];

                if (string.Compare(adaptName, searchAdapt, StringComparison.InvariantCultureIgnoreCase) == 0)
                {
                    try
                    {
                        if (nType == NetworkType.Static)
                        {
                            //게이트웨이 셋
                            ManagementBaseObject newConfig_Gateway = mObject.GetMethodParameters("SetGateways");
                            ManagementBaseObject setConfig_Gateway;
                            newConfig_Gateway["DefaultIPGateway"] = new string[] { cGateway };
                            newConfig_Gateway["GatewayCostMetric"] = new int[] { 1 };

                            setConfig_Gateway = mObject.InvokeMethod("SetGateways", newConfig_Gateway, null);

                            //아이피, 서브마스크 셋
                            ManagementBaseObject newConfig_IPAddress = mObject.GetMethodParameters("EnableStatic");
                            ManagementBaseObject setConfig_IPAddress;
                            newConfig_IPAddress["IPAddress"] = new string[] { cIPAddress };
                            newConfig_IPAddress["SubnetMask"] = new string[] { cSubnetMask };

                            setConfig_IPAddress = mObject.InvokeMethod("EnableStatic", newConfig_IPAddress, null);

                            //DNS셋
                            ManagementBaseObject newConfig_DNS = mObject.GetMethodParameters("SetDNSServerSearchOrder");
                            ManagementBaseObject setConfig_DNS;
                            newConfig_DNS["DNSServerSearchOrder"] = cDNS;
                            setConfig_DNS = mObject.InvokeMethod("SetDNSServerSearchOrder", newConfig_DNS, null);
                        }
                        else if(nType == NetworkType.Dynamic)
                        {
                            mObject.InvokeMethod("EnableDHCP", null);
                        }
                        else
                        {
                            return false;
                        }

                        return true;
                    }
                    catch
                    {
                        return false;
                    }
                }
            }

            return false;
        }
    }

我尝试了这个,但没有工作。我该如何改变网络配置?

1 个答案:

答案 0 :(得分:0)

这是正确的代码,您必须遍历mObject.Properties,并且不要忘记以“管理员”的身份启动它:

public bool SetNetworkConfiguration(string adaptName, string cIPAddress, string cSubnetMask, string cGateway, string[] cDNS, NetworkType nType)
{
  var mClass = new ManagementClass("Win32_NetworkAdapterConfiguration");
  var nCol = mClass.GetInstances();

  foreach (ManagementObject mObject in nCol)
  {
    foreach (var property in mObject.Properties)
    {
      if (property.Name == "Description")
      {
        var searchAdapt = property.Value.ToString();
        if (string.Compare(adaptName, searchAdapt, StringComparison.InvariantCultureIgnoreCase) == 0)
        {
          try
          {
            if (nType == NetworkType.Static)
            {
              //게이트웨이 셋
              var newConfig_Gateway = mObject.GetMethodParameters("SetGateways");
              ManagementBaseObject setConfig_Gateway;
              newConfig_Gateway["DefaultIPGateway"] = new[] {cGateway};
              newConfig_Gateway["GatewayCostMetric"] = new[] {1};

              setConfig_Gateway = mObject.InvokeMethod("SetGateways", newConfig_Gateway, null);

              //아이피, 서브마스크 셋
              var newConfig_IPAddress = mObject.GetMethodParameters("EnableStatic");
              ManagementBaseObject setConfig_IPAddress;
              newConfig_IPAddress["IPAddress"] = new[] {cIPAddress};
              newConfig_IPAddress["SubnetMask"] = new[] {cSubnetMask};

              setConfig_IPAddress = mObject.InvokeMethod("EnableStatic", newConfig_IPAddress, null);

              //DNS셋
              var newConfig_DNS = mObject.GetMethodParameters("SetDNSServerSearchOrder");
              ManagementBaseObject setConfig_DNS;
              newConfig_DNS["DNSServerSearchOrder"] = cDNS;
              setConfig_DNS = mObject.InvokeMethod("SetDNSServerSearchOrder", newConfig_DNS, null);
            }
            else if (nType == NetworkType.Dynamic)
            {
              mObject.InvokeMethod("EnableDHCP", null);
            }
            else
            {
              return false;
            }

            return true;
          }
          catch
          {
            return false;
          }
        }
      }
    }
  }

  return false;
}