public void SetBrowserSettings(BrowserProxySettings proxySettings) { var registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true); if (registry != null) { registry.SetValue("ProxyServer", string.Format("{0}:{1}", proxySettings.Address, proxySettings.Port)); registry.SetValue("ProxyEnable", proxySettings.SelectedProxySettings); //todo: //http://stackoverflow.com/questions/1674119/what-key-in-windows-registry-disables-ie-connection-parameter-automatically-det //http://social.technet.microsoft.com/Forums/windowsserver/en-US/5a8a47fd-ab72-488c-bfad-d8c10d18b6be/ie-lan-settings-automatically-detect-settings?forum=winserverGP } if (proxySettings.BypassProxy) { registry.SetValue("ProxyOverride", "<local>"); } else { registry.DeleteValue("ProxyOverride"); } // 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, InternetOptionSettingsChanged, IntPtr.Zero, 0); //refreshReturn = InternetSetOption(IntPtr.Zero, InternetOptionRefresh, IntPtr.Zero, 0); }
//user_pref("network.proxy.share_proxy_settings", true); public BrowserProxySettings GetBrowserProxy() { var data = new BrowserProxySettings(); try { var files = GetProfileFiles(); foreach (var profilePath in files) { int portNum; int proxyType; var text = File.ReadAllText(string.Format("{0}\\{1}", profilePath, PreferencesFileName)); data.Address = text.FindValue(ServerKey, "\");"); if (Int32.TryParse(text.FindValue(PortKey, ");"), out portNum)) { data.Port = portNum; } data.SelectedProxySettings = Int32.TryParse(text.FindValue(ProxyTypeKey, ");"), out proxyType) ? proxyType : UseSystemProxyType; } return(data); } catch (Exception) { throw; } }
// private static bool settingsReturn, refreshReturn; public BrowserProxySettings GetBrowserProxy() { var browserProxy = new BrowserProxySettings(); var registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true); if (registry != null) { var proxyRecord = registry.GetValue("ProxyServer"); if (proxyRecord != null && !string.IsNullOrEmpty(proxyRecord.ToString())) { var proxyParts = proxyRecord.ToString().Split(':'); browserProxy.Address = proxyParts[0]; if (proxyParts.Count() == 2) { int number; if (Int32.TryParse(proxyParts[1], out number)) { browserProxy.Port = number; } } } var proxyEnable = (int)registry.GetValue("ProxyEnable"); browserProxy.SelectedProxySettings = proxyEnable; var bypassProxy = (string)registry.GetValue("ProxyOverride"); browserProxy.BypassProxy = !string.IsNullOrEmpty(bypassProxy) && bypassProxy == "<local>"; } return(browserProxy); }
public void SetBrowserSettings(BrowserProxySettings proxySettings) { try { TryUnlockSelectedProxySettings(); var files = GetProfileFiles(); foreach (var profilePath in files) { var filePath = string.Format("{0}\\{1}", profilePath, PreferencesFileName); var content = File.ReadAllText(filePath); content = content.SetValue(proxySettings.Address, ServerKey, "\");"); content = content.SetValue(string.Format("{0}", proxySettings.Port), PortKey, ");"); content = content.SetValue(string.Format("{0}", proxySettings.SelectedProxySettings), ProxyTypeKey, ");"); //TODO: backup this value content = content.SetValue("true", ShareNetworkProxySettings, ");"); //TODO: backup following values content = content.SetValue(string.Format("{0}", proxySettings.Address), "user_pref(\"network.proxy.ftp\", \"", "\");"); content = content.SetValue(string.Format("{0}", proxySettings.Port), "user_pref(\"network.proxy.ftp_port\", ", ");"); content = content.SetValue(string.Format("{0}", proxySettings.Address), "user_pref(\"network.proxy.socks\", \"", "\");"); content = content.SetValue(string.Format("{0}", proxySettings.Port), "user_pref(\"network.proxy.socks_port\", ", ");"); content = content.SetValue(string.Format("{0}", proxySettings.Address), "user_pref(\"network.proxy.ssl\", \"", "\");"); content = content.SetValue(string.Format("{0}", proxySettings.Port), "user_pref(\"network.proxy.ssl_port\", ", ");"); foreach (var process in System.Diagnostics.Process.GetProcessesByName("firefox")) { process.Kill(); } File.Delete(filePath); File.WriteAllText(filePath, content); } } catch (Exception) { throw; } }