//METHODS public Boolean AddProxySettingsData(ProxySettingsData proxySettingsData) { if (proxySettingsData.Validate()) { proxySettingsDataList.Add(proxySettingsData); return true; } else { return false; } }
//EVENTS private void saveButton_Click(object sender, EventArgs e) { ProxySettingsData proxySettingsData = new ProxySettingsData(); proxySettingsData.BypassProxyForLocalAddress = bypassProxyCheckBox.Checked; proxySettingsData.Exceptions = (exceptionsTextBox.Text.Length == 0 ? null : exceptionsTextBox.Text); proxySettingsData.ProxyEnable = true; proxySettingsData.UseSameProxyServerForAllProtocols = useSameProxyCheckBox.Checked; if (ftpProxyAddressTextBox.Text.Length > 0) { IPEndPoint ipEndPoint; if (IpEndPointFactory.BuildIpEndPoint(ftpProxyAddressTextBox.Text, ftpProxyPortTextBox.Text, out ipEndPoint)) { proxySettingsData.FtpProtocolIpEndPoint = ipEndPoint; } else { MessageBox.Show(MessageBoxResource.Invalid_FtpAddress, MessageBoxResource.Caption, MessageBoxButtons.OK, MessageBoxIcon.Asterisk); return; } } if (httpProxyPortTextBox.Text.Length > 0) { IPEndPoint ipEndPoint; if (IpEndPointFactory.BuildIpEndPoint(httpProxyAddressTextBox.Text, httpProxyPortTextBox.Text, out ipEndPoint)) { proxySettingsData.HttpProtocolIpEndPoint = ipEndPoint; } else { MessageBox.Show(MessageBoxResource.Invalid_HttpAddress, MessageBoxResource.Caption, MessageBoxButtons.OK, MessageBoxIcon.Asterisk); return; } } if (httpsProxyPortTextBox.Text.Length > 0) { IPEndPoint ipEndPoint; if (IpEndPointFactory.BuildIpEndPoint(httpsProxyAddressTextBox.Text, httpsProxyPortTextBox.Text, out ipEndPoint)) { proxySettingsData.HttpsProtocolIpEndPoint = ipEndPoint; } else { MessageBox.Show(MessageBoxResource.Invalid_HttpsAddress, MessageBoxResource.Caption, MessageBoxButtons.OK, MessageBoxIcon.Asterisk); return; } } if (proxyServerAddressTextBox.Text.Length > 0) { IPEndPoint ipEndPoint; if (IpEndPointFactory.BuildIpEndPoint(proxyServerAddressTextBox.Text, proxyServerPortTextBox.Text, out ipEndPoint)) { proxySettingsData.ProxyServerIpEndPoint = ipEndPoint; } else { MessageBox.Show(MessageBoxResource.Invalid_ProxyAddress, MessageBoxResource.Caption, MessageBoxButtons.OK, MessageBoxIcon.Asterisk); return; } } if (socksProxyAddressTextBox.Text.Length > 0) { IPEndPoint ipEndPoint; if (IpEndPointFactory.BuildIpEndPoint(socksProxyAddressTextBox.Text, socksProxyPortTextBox.Text, out ipEndPoint)) { proxySettingsData.SocksProtocolIpEndPoint = ipEndPoint; } else { MessageBox.Show(MessageBoxResource.Invalid_SocksAddress, MessageBoxResource.Caption, MessageBoxButtons.OK, MessageBoxIcon.Asterisk); return; } } if (proxySettingsData.Validate()) { if (proxyProfilesProcess.AddProxySettingsData(proxySettingsData)) { if (proxyProfilesProcess.WriteProxySettingsDataList()) { MessageBox.Show(MessageBoxResource.Success_ProxyProfilesSaved, MessageBoxResource.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information); Close(); } else { MessageBox.Show(MessageBoxResource.Error_WriteProfiles, MessageBoxResource.Caption, MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } } else { MessageBox.Show(MessageBoxResource.Error_AddProfile, MessageBoxResource.Caption, MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } } else { MessageBox.Show(MessageBoxResource.Invalid_ProxySettingsData, MessageBoxResource.Caption, MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } }
public static Boolean SetProxySettings(ProxySettingsData proxySettingsData) { if (proxySettingsData.Validate()) { try { Registry.SetValue(ProxySettingsProcessResource.KeyName_InternetSettings, ProxySettingsProcessResource.ValueName_ProxyEnable, (proxySettingsData.ProxyEnable.Value ? 1 : 0)); if (proxySettingsData.ProxyOverride == null) { Object proxyOverrideValueObject; proxyOverrideValueObject = Registry.GetValue(ProxySettingsProcessResource.KeyName_InternetSettings, ProxySettingsProcessResource.ValueName_ProxyOverride, null); if (proxyOverrideValueObject != null) { RegistryKey softwareRegistryKey = Registry.CurrentUser.OpenSubKey(ProxySettingsProcessResource.SubKeyName_Software); RegistryKey mircosoftRegistryKey = softwareRegistryKey.OpenSubKey(ProxySettingsProcessResource.SubKeyName_Microsoft); RegistryKey windowsRegistryKey = mircosoftRegistryKey.OpenSubKey(ProxySettingsProcessResource.SubKeyName_Windows); RegistryKey currentVersionRegistryKey = windowsRegistryKey.OpenSubKey(ProxySettingsProcessResource.SubKeyName_CurrentVersion); RegistryKey internetSettings = currentVersionRegistryKey.OpenSubKey(ProxySettingsProcessResource.SubKeyName_InternetSettings, true); internetSettings.DeleteValue(ProxySettingsProcessResource.Value_ProxyOverride, true); internetSettings.Close(); currentVersionRegistryKey.Close(); windowsRegistryKey.Close(); mircosoftRegistryKey.Close(); softwareRegistryKey.Close(); } } else { Registry.SetValue(ProxySettingsProcessResource.KeyName_InternetSettings, ProxySettingsProcessResource.ValueName_ProxyOverride, proxySettingsData.ProxyOverride); } Registry.SetValue(ProxySettingsProcessResource.KeyName_InternetSettings, ProxySettingsProcessResource.ValueName_ProxyServer, proxySettingsData.ProxyServer); } catch (Exception exception) { EnterpriseLibraryLogWriter.WriteExceptionLogEntry(exception); return false; } } else { return false; } return true; }