20

I'm writing a Winform's (C# .NET) app to change Windows' Global (aka Internet Explorer's) proxy settings.

I'm using this.

RegistryKey registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
registry.SetValue("ProxyEnable", 1);
registry.SetValue("ProxyServer", "127.0.0.1:8080");

But its behaving in a weird manner. I tested this using two browsers

  • Google Chrome:

When I change/Disable the proxy while Chrome is running. Chrome is still using the previous proxy. The change is not effecting its process. But when I JUST open Internet Options(inetcpl.cpl) > Connections > LAN Settings. The previous change of proxy is now considered. When I said Just open I really mean Just open. I mean, not editing or clicking any other buttons. I guess, its then the global proxy is really getting changed (by reading from registry) & Google Chrome is immediately taking the effect.

  • Internet Explorer 8:

Case with Internet Explorer is much worse. After changing/disabling the proxy using my app while IE is running & Even after going to "Internet Options(inetcpl.cpl) > Connections > Lan Settings" The running IE proxy isn't getting affected. Not even if I open a new link in a new tab. I had to restart IE for that change to be incorporated.

The behavior I want is that whenever I change proxy settings in my app, all the browsers which are using global proxy (irrespective of whether they are running or not) should instantly incorporate the change in settings.

How can I achieve this?

T.Rob
  • 31,522
  • 9
  • 59
  • 103
claws
  • 52,236
  • 58
  • 146
  • 195
  • look at this page. http://stackoverflow.com/questions/5216323/setting-ie-proxy-by-c-sharp – Ebrahim Rad Jul 16 '13 at 23:17
  • For IE: [https://stackoverflow.com/questions/197725/programmatically-set-browser-proxy-settings-in-c](https://stackoverflow.com/questions/197725/programmatically-set-browser-proxy-settings-in-c) – A G Jan 07 '10 at 13:47

1 Answers1

26

The behavior I want is that when ever I change proxy settings in my app, all the browsers which are using global proxy (irrespective of whether they are running or not) should instantly incorporate the change in settings.

How can I achieve this?

You need to refresh your system to achieve that.

Add these lines at the beginning of your code:

using System.Runtime.InteropServices;
using Microsoft.Win32;

Add this in the beginning of your class:

[DllImport("wininet.dll")]
public static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength);
public const int INTERNET_OPTION_SETTINGS_CHANGED = 39;
public const int INTERNET_OPTION_REFRESH = 37;
static bool settingsReturn, refreshReturn;

And imply the code:

RegistryKey registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
registry.SetValue("ProxyEnable", 1);
registry.SetValue("ProxyServer", YOURPROXY);

// 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);
claws
  • 52,236
  • 58
  • 146
  • 195
  • Calling this code to set the proxy works, but a subsequent call to reset the previous values (which I pulled and stored from the registry) from the same program has no effect. Any ideas? – Petrus Theron Dec 01 '10 at 07:23
  • 1
    @claws The code works well. but how to write the credentials? "username" & "password" ? – Dark_Knight Nov 14 '14 at 12:44
  • 1
    This code is quite good... It does not work on windows 10 for sure. It does not update Proxy settings in UI and proxy itself does not work after this – Nikas Žalias Apr 25 '19 at 11:32
  • 2
    @NikasŽalias I just use the above code and it works on windows 10. – Jeremy Su Jul 10 '19 at 11:36