예제 #1
0
        /// <summary>
        /// Switch from dhcp to static mode
        /// </summary>
        /// <param name="uuid"></param>
        public void SwitchDhcpToStaticMode(string uuid)
        {
            ManagementClass            manageClass   = new ManagementClass(AppConfiguration.NET_CONFIG);
            ManagementObjectCollection objCollection = manageClass.GetInstances();

            foreach (ManagementObject item in objCollection)
            {
                if ((bool)item[AppConfiguration.IP_ENABLED_KEY] && (string)item[AppConfiguration.SETTING_ID_KEY] == uuid)
                {
                    AdapterEntity adapter = new AdapterEntity();
                    adapter = GetAdapterInfo(uuid);

                    ManagementBaseObject objNewIP   = item.GetMethodParameters(AppConfiguration.ENABLE_STATIC_KEY);
                    ManagementBaseObject objNewGate = item.GetMethodParameters(AppConfiguration.SET_GATEWAYS_KEY);

                    //Set DefaultGateway
                    objNewGate[AppConfiguration.DEFAULT_IP_GATEWAY_KEY]  = new string[] { adapter.DefaultIpGateway };
                    objNewGate[AppConfiguration.GATEWAY_COST_METRIC_KEY] = new int[] { 1 };

                    //Set IPAddress and Subnet Mask
                    objNewIP[AppConfiguration.IP_ADDRESS_KEY]  = new string[] { adapter.IpAddress };
                    objNewIP[AppConfiguration.SUBNET_MASK_KEY] = new string[] { adapter.NetMask };

                    ManagementBaseObject objSetIP = item.InvokeMethod(AppConfiguration.ENABLE_STATIC_KEY, objNewIP, null);
                    objNewGate = item.InvokeMethod(AppConfiguration.SET_GATEWAYS_KEY, objNewGate, null);
                }
            }
        }
예제 #2
0
        private AdapterEntity SwitchInterfaceProperties(PropertyDataCollection networkAdapterProperties)
        {
            AdapterEntity adapterEntity = new AdapterEntity();

            foreach (PropertyData networkAdapterProperty in networkAdapterProperties)
            {
                switch (networkAdapterProperty.Name)
                {
                case "IPAddress":
                    foreach (string ipAddress in (string[])networkAdapterProperty.Value)
                    {
                        adapterEntity.IpAddress = ipAddress;
                        break;
                    }
                    break;

                case "IPSubnet":
                    foreach (string netMask in (string[])networkAdapterProperty.Value)
                    {
                        adapterEntity.NetMask = netMask;
                        break;
                    }
                    break;

                case "DefaultIPGateway":
                    if ((string[])networkAdapterProperty.Value != null)
                    {
                        foreach (string defaultIPGateway in (string[])networkAdapterProperty.Value)
                        {
                            adapterEntity.DefaultIpGateway = defaultIPGateway;
                            break;
                        }
                    }
                    else
                    {
                        adapterEntity.DefaultIpGateway = "";
                    }
                    break;

                case "SettingID":
                    adapterEntity.UUID = (string)networkAdapterProperty.Value;
                    break;

                case "DHCPEnabled":
                    if (networkAdapterProperty.Value != null)
                    {
                        adapterEntity.IsDhcpEnabled = (bool)networkAdapterProperty.Value;
                    }
                    else
                    {
                        adapterEntity.IsDhcpEnabled = false;
                    }
                    break;

                case "DNSServerSearchOrder":
                    if ((string[])networkAdapterProperty.Value != null)
                    {
                        adapterEntity.DNS = (string[])networkAdapterProperty.Value;
                    }
                    else
                    {
                        adapterEntity.DNS = new string[] { "", "" }
                    };
                    break;

                default:
                    break;
                }
            }
            return(adapterEntity);
        }