none
How to change Global Windows Proxy using C# .NET with `Immediate Effect`

    Question

  • I'm writing a winform's (C# .NET) app to change Window's 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 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(inet.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(inet.cpl) > Connections > Lan Settings" The running IE proxy isn't getting effected. Not even if I open a new link in a new tab. I had to restart IE for that change to be incorporated.

    I 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?

     

    Thursday, January 07, 2010 2:28 PM

Answers

  • Hi.
    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;

            [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;
            bool settingsReturn, refreshReturn;
    
    And imply the code:

                RegKey.SetValue("ProxyServer", YOURPROXY);
                RegKey.SetValue("ProxyEnable", 1);
    
                // 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);


    Noam B
    _________________________________________________________

    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
    • Marked as answer by Harry Zhu Thursday, January 14, 2010 2:39 AM
    Thursday, January 07, 2010 3:32 PM

All replies

  • Hi.
    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;

            [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;
            bool settingsReturn, refreshReturn;
    
    And imply the code:

                RegKey.SetValue("ProxyServer", YOURPROXY);
                RegKey.SetValue("ProxyEnable", 1);
    
                // 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);


    Noam B
    _________________________________________________________

    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
    • Marked as answer by Harry Zhu Thursday, January 14, 2010 2:39 AM
    Thursday, January 07, 2010 3:32 PM
  • Hi there,

    I am using this code to change my proxy but I think I have the same problem. I need to change proxy in my application that uses Windows Forms's web browser component when I click the button.

    My button code looks like this:

                RegistryKey RegKey = Registry.CurrentUser.OpenSubKey(@"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);

                if (action == true)
                {
                    RegKey.SetValue("ProxyServer", strProxy);
                    RegKey.SetValue("ProxyEnable", 1);
                } else {
                    RegKey.SetValue("ProxyEnable", 0);
                }

                settingsReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0);
                refreshReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);

    When I click it first time it changes the proxy and seems to not want to change it again to different proxy. I really need to change proxy multiple times in this application without closing it.

    Of course I have:

            [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;
            bool settingsReturn, refreshReturn;

    at the begining of the Class.

    Thursday, July 15, 2010 12:39 PM
  • When I click it first time it changes the proxy and seems to not want to change it again to different proxy. I really need to change proxy multiple times in this application without closing it.

    Same for me... Does anyone have an idea?
    • Proposed as answer by AlgoRhymes Monday, August 02, 2010 7:46 AM
    Tuesday, July 20, 2010 7:43 PM
  • I did it by placing any necessary configuration into a text file and reading it every time an application starts, then doing

            System.Windows.Forms.Application.Restart();

    It's not elegant and it doesn't solve the problem but it helped me.

     

    Note: Opening multiple Windows Forms and closing previous ones doesn't solve the problem. Proxy is still the same.

    Monday, August 02, 2010 7:50 AM
  • Very interesting.  I am seeing the same behavior.  I can only change the proxy once without having to restart my application.  In my case the application is a taskbar tool that allows me to change the proxy between various proxies I use for different networks I connect to.  There's not much harm in my taskbar app restarting istelf, but for some other application this may be very undesireable.
    Tuesday, June 07, 2011 2:31 PM
  • @Algorhymes by using ur  code.. i  am able  to change proxy setting OF LAN SETTING in Internet explorer..

     

    But i  want  to change my DLS CONNECTION "DREAMNET" proxy through  this code..

     

    Sunday, December 18, 2011 2:02 PM
  • You haven't defined RegKey identifier.

    May be something like:

    RegistryKey RegKey = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);

    Saturday, April 13, 2013 10:17 AM