示例#1
0
        /// <summary>
        /// Writes the data into registry.
        /// </summary>
        /// <param name="card">The card.</param>
        public static void WriteDataIntoRegistry(WindowsNetworkCard card)
        {
            RegistryKey regKey = null;

            string sKey = @"SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\" + card.Id;

            try
            {
                regKey = Registry.LocalMachine.OpenSubKey(sKey);

                RegistryUtility.WriteIntValue(RegistryKeyType.LocalMachine, sKey, "EnableDhcp", card.Dhcp ? 1 : 0);

                if (card.Dhcp)
                {
                    String[] temp = { "0.0.0.0" };
                    RegistryUtility.WriteMultiStringValue(RegistryKeyType.LocalMachine, sKey, "IPAddress", temp);
                    RegistryUtility.WriteMultiStringValue(RegistryKeyType.LocalMachine, sKey, "SubnetMask", temp);

                    temp[0] = "";
                    RegistryUtility.WriteMultiStringValue(RegistryKeyType.LocalMachine, sKey, "DefaultGateway", temp);
                }
                else
                {
                    String[] temp = { "0.0.0.0" };

                    temp[0] = card.IpAddress;
                    RegistryUtility.WriteMultiStringValue(RegistryKeyType.LocalMachine, sKey, "IPAddress", temp);
                    temp[0] = card.SubnetMask;
                    RegistryUtility.WriteMultiStringValue(RegistryKeyType.LocalMachine, sKey, "SubnetMask", temp);
                    temp[0] = card.GatewayAddress;
                    RegistryUtility.WriteMultiStringValue(RegistryKeyType.LocalMachine, sKey, "DefaultGateway", temp);
                }

                string dns = "";
                if (!card.DynamicDNS)
                {
                    if (card.Dns.Length >= 1)
                    {
                        dns = card.Dns;
                    }
                    if (card.Dns2.Length >= 1)
                    {
                        dns += "," + card.Dns2;
                    }
                }
                RegistryUtility.WriteStringValue(RegistryKeyType.LocalMachine, sKey, "NameServer", dns);
            }
            finally
            {
                if (regKey != null)
                {
                    regKey.Close();
                }
            }
        }
示例#2
0
        /// <summary>
        /// Applies the config.
        /// </summary>
        /// <param name="config">The config.</param>
        /// <returns></returns>
        public static bool ApplyConfig(ProxyConfiguration config)
        {
            string szValue = string.Empty;

            if (config == null)
            {
                return(false);
            }

            if (config.Enabled)
            {
                string tmpServerAddress = config.ServerAddress;
                if (config.Port != 0)
                {
                    tmpServerAddress += ":" + config.Port;
                }

                string tmpOverride = config.ProxyOverride;
                if (tmpOverride == null)
                {
                    tmpOverride = "";
                }

                if (config.ProxyOverrideEnabled)
                {
                    if (!tmpOverride.Contains("<local>"))
                    {
                        tmpOverride += ";<local>";
                    }
                }
                else
                {
                    tmpOverride = tmpOverride.Replace("<local>", "");

                    if (tmpOverride.EndsWith(";"))
                    {
                        tmpOverride = tmpOverride.Substring(0, tmpOverride.Length - 1);
                    }

                    tmpOverride = tmpOverride.Replace(";;", ";");
                }
                RegistryUtility.WriteIntValue(RegistryKeyType.CurrentUser, RK_INTERNET_SETTINGS, "ProxyEnable", 1);
                RegistryUtility.WriteStringValue(RegistryKeyType.CurrentUser, RK_INTERNET_SETTINGS, "ProxyServer", tmpServerAddress);
                RegistryUtility.WriteStringValue(RegistryKeyType.CurrentUser, RK_INTERNET_SETTINGS, "ProxyOverride", tmpOverride);
            }
            else
            {
                RegistryUtility.WriteIntValue(RegistryKeyType.CurrentUser, RK_INTERNET_SETTINGS, "ProxyEnable", 0);
            }

            // set Immediate proxy settings changing (Chrome bug)
            // http://stackoverflow.com/questions/2020363/how-to-change-global-windows-proxy-using-c-sharp-net-with-immediate-effect
            bool settingsReturn, refreshReturn;

            // These lines implement the Interface in the beginning of program
            // They cause the OS to refresh the settings, causing IP to realy update
            settingsReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0);
            refreshReturn  = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);

            return(true);
        }