private void BtnSaveClick(object sender, System.EventArgs e)
        {
            if (CheckForDuplicatedConfigurationName(txtConfigAlias.Text.Trim()) && _currentNetworkConfigurationBackup == null)
            {
                MessageBox.Show(NodNetworkHelperResources.msgConfigurationNameAlreadyExist, NetworkConfigurationController.APPLICATION_NAME,
                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                if (ValidateConfigurationToSave())
                {
                    var configurationToSave = new NetworkConfiguration
                    {
                        ConfigurationName = txtConfigAlias.Text.Trim(),
                        UseDHCP = chkDHCP.Checked,
                        UseProxy = chkUseProxy.Checked,
                        BypassForLocalAddress = chkByPassForLocal.Checked,
                        IpAddress = txtIP.Text.Trim(),
                        SubNetworkMask = txtSubnetMask.Text.Trim(),
                        DefaultGateway = txtGateway.Text.Trim(),
                        PreferentialDNS = txtDNS1.Text.Trim(),
                        AlternativeDNS = txtDNS2.Text.Trim(),
                        Proxy = txtProxy.Text.Trim(),
                        WifiNameToWatch = txtWifiAssociated.Text.Trim(),
                        NetworkAdapter = cmbNetworkAdapter.SelectedItem.ToString()
                    };

                    _networkConfigurationController.AddNetworkConfiguration(configurationToSave, _currentNetworkConfigurationBackup);
                    LoadConfigurationsList();
                    ClearAllFields();
                    _currentNetworkConfigurationBackup = null;
                    DisableAllEdit();
                }
            }
        }
 private void BtnNewClick(object sender, System.EventArgs e)
 {
     ClearAllFields();
     _currentNetworkConfigurationBackup = null;
     EnableAllEdit();
     cmbConfiguration.SelectedIndex = -1;
 }
        private void BtnDelClick(object sender, System.EventArgs e)
        {
            if (cmbConfiguration.SelectedItem == null) { return; }
            if (DialogResult.Yes != MessageBox.Show(NodNetworkHelperResources.msgDeleteConfiguration, NetworkConfigurationController.APPLICATION_NAME, MessageBoxButtons.YesNo,
                MessageBoxIcon.Question)) { return; }

            _currentNetworkConfigurationBackup = null;
            _networkConfigurationController.RemoveNetworkConfiguration(cmbConfiguration.SelectedItem.ToString());
            ClearAllFields();
            LoadConfigurationsList();
            DisableAllEdit();
        }
 private void BtnUndoClick(object sender, System.EventArgs e)
 {
     if (_currentNetworkConfigurationBackup == null || string.IsNullOrEmpty(_currentNetworkConfigurationBackup.ConfigurationName))
     {
         _currentNetworkConfigurationBackup = null;
         ClearAllFields();
     }
     else
     {
         LoadConfigurationDetails(_currentNetworkConfigurationBackup);
     }
 }
        private void LoadConfigurationDetails(NetworkConfiguration netConfig)
        {
            chkDHCP.Checked = netConfig.UseDHCP;
            chkUseProxy.Checked = netConfig.UseProxy;
            chkByPassForLocal.Checked = netConfig.BypassForLocalAddress;

            txtConfigAlias.Text = netConfig.ConfigurationName;
            txtIP.Text = netConfig.IpAddress;
            txtSubnetMask.Text = netConfig.SubNetworkMask;
            txtGateway.Text = netConfig.DefaultGateway;
            txtDNS1.Text = netConfig.PreferentialDNS;
            txtDNS2.Text = netConfig.AlternativeDNS;
            txtProxy.Text = netConfig.Proxy;
            txtWifiAssociated.Text = netConfig.WifiNameToWatch;

            cmbNetworkAdapter.SelectedIndex = cmbNetworkAdapter.Items.IndexOf(netConfig.NetworkAdapter);

            if (netConfig.UseDHCP)
            {
                DisableAllEdit();
                txtConfigAlias.Enabled = true;
                chkDHCP.Enabled = true;
                cmbNetworkAdapter.Enabled = true;
                txtWifiAssociated.Enabled = true;
                btnSave.Enabled = true;
                btnUndo.Enabled = true;
            }
            else if (!netConfig.UseProxy)
            {
                txtProxy.Enabled = false;
                chkByPassForLocal.Enabled = false;
            }
        }
        private void CmbConfigurationSelectedIndexChanged(object sender, System.EventArgs e)
        {
            if (cmbConfiguration.SelectedItem == null)
            {
                _currentNetworkConfigurationBackup = null;
            }
            else
            {
                _currentNetworkConfigurationBackup = _networkConfigurationController.NetworkConfigurationsList.FirstOrDefault(x =>
                    x.ConfigurationName == cmbConfiguration.SelectedItem.ToString());
            }

            if (_currentNetworkConfigurationBackup == null || string.IsNullOrEmpty(_currentNetworkConfigurationBackup.ConfigurationName))
            {
                ClearAllFields();
            }
            else
            {
                EnableAllEdit();
                LoadConfigurationDetails(_currentNetworkConfigurationBackup);
            }
        }
        public void AddNetworkConfiguration(NetworkConfiguration configurationToAdd, NetworkConfiguration oldConfigurationReference)
        {
            if (oldConfigurationReference == null)
            {
                NetworkConfigurationsList.Add(configurationToAdd);
            }
            else
            {
                var configurationToUpdate = NetworkConfigurationsList.Single(x => x.ConfigurationName == oldConfigurationReference.ConfigurationName);
                NetworkConfigurationsList.Remove(configurationToUpdate);
                NetworkConfigurationsList.Add(configurationToAdd);
            }

            if (_updateNetworkContextMethod != null) { _updateNetworkContextMethod(); }
            SaveNetworkConfiguration();
        }
        private void SetIPtoDesiredNetworkAdapter(NetworkConfiguration configurationToSet)
        {
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            var moc = mc.GetInstances();

            foreach (ManagementObject mo in moc)
            {
                if (!mo["Caption"].Equals(configurationToSet.NetworkAdapter)) { continue; }

                ManagementBaseObject newIP = mo.GetMethodParameters("EnableStatic");
                ManagementBaseObject newGateway = mo.GetMethodParameters("SetGateways");
                ManagementBaseObject newDNS = mo.GetMethodParameters("SetDNSServerSearchOrder");

                newGateway["DefaultIPGateway"] = new[] { configurationToSet.DefaultGateway };
                newGateway["GatewayCostMetric"] = new[] { 1 };

                newIP["IPAddress"] = configurationToSet.IpAddress.Split(',');
                newIP["SubnetMask"] = new[] { configurationToSet.SubNetworkMask };

                var dnsIPs = string.Format("{0},{1}", configurationToSet.PreferentialDNS, configurationToSet.AlternativeDNS);
                newDNS["DNSServerSearchOrder"] = dnsIPs.Split(',');

                mo.InvokeMethod("EnableStatic", newIP, null);
                mo.InvokeMethod("SetGateways", newGateway, null);
                mo.InvokeMethod("SetDNSServerSearchOrder", newDNS, null);

                break;
            }
        }
        public void SetNetworkConfiguration(NetworkConfiguration configurationToSet)
        {
            _busyWithConfigurationChange = true;

            try
            {
                if (configurationToSet.UseDHCP)
                {
                    SetDHCPtoDesiredNetworkAdapter(configurationToSet.NetworkAdapter);
                }
                else
                {
                    SetIPtoDesiredNetworkAdapter(configurationToSet);
                }

                _displayInfoMethod(string.Format(NodNetworkHelperResources.msgConfigurationChangedTo, configurationToSet.ConfigurationName));
            }
            catch (Exception)
            {
                _displayErrorMethod(string.Format(NodNetworkHelperResources.msgFailureConfigurationChangedTo, configurationToSet.ConfigurationName));
            }

            if (_nodNetworkHelperData.WifiWatcherEnabled)
            {
                // Wait for a while to allow the adapter to complete the configuration change and do not trigger another connection change in the WifiWatcher
                Thread.Sleep(5000);
            }

            _busyWithConfigurationChange = false;
        }