Exemplo n.º 1
0
        private void SaveDataToRegistry(NetworkInterfaceSettings settings, string controlSet)
        {
            var key =
                Registry.LocalMachine.CreateSubKey("SYSTEM\\" + controlSet + "\\Services\\Tcpip\\Parameters\\Interfaces\\" +
                                                   settings.SettingId);

            if (key != null)
            {
                var multi = new string[1];
                if (settings.IsDHCP)
                {
                    multi[0] = "";
                    key.SetValue("EnableDHCP", 1);
                    key.SetValue("IPAddress", multi, RegistryValueKind.MultiString);
                    key.SetValue("SubnetMask", multi, RegistryValueKind.MultiString);
                    key.SetValue("DefaultGateway", multi, RegistryValueKind.MultiString);
                }
                else
                {
                    key.SetValue("EnableDHCP", 0);
                    multi[0] = settings.IP.ToString();
                    key.SetValue("IPAddress", multi, RegistryValueKind.MultiString);
                    multi[0] = settings.Mask.ToString();
                    key.SetValue("SubnetMask", multi, RegistryValueKind.MultiString);
                    multi[0] = settings.GateWay.ToString();
                    key.SetValue("DefaultGateway", multi, RegistryValueKind.MultiString);
                }
            }
        }
Exemplo n.º 2
0
 public NetworkInterfaceSettings(NetworkInterfaceSettings other)
 {
     SettingId = other.SettingId;
     Name      = other.Name;
     IsDHCP    = other.IsDHCP;
     IsDNSDHCP = other.IsDNSDHCP;
     IP        = new IPAddress(other.IP);
     GateWay   = new IPAddress(other.GateWay);
     Mask      = new IPAddress(other.Mask);
     DNS1      = new IPAddress(other.DNS1);
     DNS2      = new IPAddress(other.DNS2);
 }
Exemplo n.º 3
0
        private void LoaderDoWork(object o, DoWorkEventArgs e)
        {
            var worker = (BackgroundWorker)o;

            connections.Clear();
            try {
                ManagementObjectCollection adaptersCollection = LoadAdapters();
                if (adaptersCollection == null)
                {
                    return;
                }

                foreach (ManagementObject objMo in adaptersCollection)
                {
                    if (worker.CancellationPending)
                    {
                        e.Cancel = true;
                        connections.Clear();
                        waitingEvent.Set();
                        return;
                    }
                    try {
                        var If = new NetworkInterfaceSettings(new AdapterDataHelper(objMo));
                        connections.Add(If);
                    }
                    catch (Exception ex) {
                        Trace.WriteLine(ex.Source);
                        Trace.WriteLine(ex.Data);
                    }
                }
            }
            catch (NullReferenceException ex) {
                Trace.WriteLine(ex.Source);
                Trace.WriteLine(ex.Data);
                throw new NullReferenceException("error", ex);
            }
            catch (Exception ex) {
                Trace.WriteLine(ex.Source);
                Trace.WriteLine(ex.Data);
            }
            finally {
                waitingEvent.Set();
            }
        }
Exemplo n.º 4
0
        public bool Save(NetworkInterfaceSettings settings)
        {
            ManagementObjectCollection adaptersCollection;

            using (var objMC = new ManagementObjectSearcher()) {
                objMC.Query = new ObjectQuery("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True");

                adaptersCollection = objMC.Get();
            }

            foreach (ManagementObject objMo in adaptersCollection)
            {
                if (Convert.ToBoolean(objMo["ipEnabled"]) == false)
                {
                    continue;
                }

                if (settings.SettingId != (string)objMo["SettingID"])
                {
                    continue;
                }

                //IP
                if (settings.IsDHCP)
                {
                    objMo.InvokeMethod("EnableDHCP", null, null);
                }
                else
                {
                    ManagementBaseObject objNewIP   = objMo.GetMethodParameters("EnableStatic");
                    ManagementBaseObject objNewGate = objMo.GetMethodParameters("SetGateways");

                    objNewGate["DefaultIPGateway"]  = new[] { settings.GateWay.ToString() };
                    objNewGate["GatewayCostMetric"] = new[] { 1 };

                    objNewIP["IPAddress"]  = new[] { settings.IP.ToString() };
                    objNewIP["SubnetMask"] = new[] { settings.Mask.ToString() };

                    objMo.InvokeMethod("EnableStatic", objNewIP, null);
                    objMo.InvokeMethod("SetGateways", objNewGate, null);
                }

                ManagementBaseObject objNewDNS = objMo.GetMethodParameters("SetDNSServerSearchOrder");
                var buff = new string[1];

                if (settings.IsDNSDHCP)
                {
                    buff = null;
                }
                else
                {
                    if (settings.DNS2.IP[0] != 0)
                    {
                        buff    = new string[2];
                        buff[1] = settings.DNS2.ToString();
                    }
                    buff[0] = settings.DNS1.ToString();
                }
                objNewDNS["DNSServerSearchOrder"] = buff;
                objMo.InvokeMethod("SetDNSServerSearchOrder", objNewDNS, null);
            }

            SaveDataToRegistry(settings, "CurrentControlSet");
            SaveDataToRegistry(settings, "ControlSet001");

            return(true);
        }