code_junkie

Programmatically Set Browser Proxy Settings in C#

Nov 11th, 2011
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. RegistryKey registry = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Internet Settings", true);
  2. registry.SetValue("ProxyEnable", 1);
  3. registry.SetValue("ProxyServer", "127.0.0.1:8080");
  4.  
  5. [DllImport("wininet.dll")]
  6. public static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength);
  7. public const int INTERNET_OPTION_SETTINGS_CHANGED = 39;
  8. public const int INTERNET_OPTION_REFRESH = 37;
  9. bool settingsReturn, refreshReturn;
  10.  
  11. RegKey.SetValue("ProxyServer", YOURPROXY);
  12. RegKey.SetValue("ProxyEnable", 1);
  13.  
  14. // These lines implement the Interface in the beginning of program
  15. // They cause the OS to refresh the settings, causing IP to realy update
  16. settingsReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0);
  17. refreshReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);
Add Comment
Please, Sign In to add comment