9

I am writing a program to automatically switch my proxy address based on the network I am connected to.

I have so far got everything to work except the part that I have highlighted below.

LAN Settings Dialog

Is there any way to change the automatic configuration script and the automatically detect settings in code?

The solution can be either P/Invoke registry editing. I just need something that works.

Alex Essilfie
  • 12,339
  • 9
  • 70
  • 108

5 Answers5

20

You can change proxy settings by using the registry. See the following link:
http://support.microsoft.com/kb/819961

Key path: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings

Values:

"MigrateProxy"=dword:00000001
"ProxyEnable"=dword:00000001
"ProxyHttp1.1"=dword:00000000
"ProxyServer"="http://ProxyServername:80"
"ProxyOverride"="<local>"

A question in SuperUser.com regarding how to disable automatically detect settings in ie proxy configuration. Disable "Automatically detect settings" in IE proxy configuration

A snippet, taken from Internet Explorer Automatic Configuration Script Definition via Registry.

Script 1: This enables the AutoConf Script and defines what it is (exchange the http://xxxx with your script)

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings]
"AutoConfigURL"="http://xxx.xxx.xxx.xxx.xxxx"
"ProxyEnable"=dword:00000000

Script 2: This script Disables the AutoConf Script and enables a proxy server with exceptions.

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings]
"ProxyEnable"=dword:00000001
"ProxyOverride"="proxyexceptionname:portnumber;anotherexceptionname:port
"ProxyServer"="ftp=MyFTPProxy:Port;http=MYHTTPPROXY:PORT;https=MYHTTPSPROXY:PORT
"AutoConfigURL"=""
Community
  • 1
  • 1
ichen
  • 512
  • 4
  • 12
  • Thanks for the info on the `AutoConfigUrl`. That was one thing I was looking for. I found how to disable/enable the other check box (*Automatically detect settings*) by reading [Andrew Swan's comment to the SuperUser.com question that you linked](http://superuser.com/questions/79703/disable-automatically-detect-settings-in-ie-proxy-configuration/79718#comment-217210). It basically says subtract 8 from the ninth byte of `HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections|DefaultConnectionSettings` to disable the option. – Alex Essilfie Apr 06 '11 at 19:37
8

I searched all through for this. But as I couldnt find, I had written the below code snippete that works for this purpose.

    /// <summary>
    /// Checks or unchecks the IE Options Connection setting of "Automatically detect Proxy"
    /// </summary>
    /// <param name="set">Provide 'true' if you want to check the 'Automatically detect Proxy' check box. To uncheck, pass 'false'</param>
    public void IEAutoDetectProxy(bool set)
    {
        // Setting Proxy information for IE Settings.
        RegistryKey RegKey = Registry.CurrentUser.OpenSubKey(@"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Connections", true);
        byte[] defConnection = (byte[])RegKey.GetValue("DefaultConnectionSettings");
        byte[] savedLegacySetting = (byte[])RegKey.GetValue("SavedLegacySettings");
        if (set)
        {
            defConnection[8] = Convert.ToByte(9);
            savedLegacySetting[8] = Convert.ToByte(9);
        }
        else
        {
            defConnection[8] = Convert.ToByte(1);
            savedLegacySetting[8] = Convert.ToByte(1);
        }
        RegKey.SetValue("DefaultConnectionSettings", defConnection);
        RegKey.SetValue("SavedLegacySettings", savedLegacySetting);
    }
j0k
  • 22,600
  • 28
  • 79
  • 90
2

I am answering because I am not allowed to comment on answers. I would like to point out a difference between manipulating registry vs using InternetSetOptionAPI. If you directly poke registry to change proxy settings then browsers like Chrome that depends on WinInet proxy configuration won't immediately pickup the new settings but if you change using InternetSetOptionAPI the new settings will be used immediately. This is my experience. I did not go into the details to find out what can be done to pickup the settings after manipulating the registry.

EDIT: In order to refresh the WinInet proxy settings you can do a simple PInvoke of InternetSetOption API as follows

internal class InternetSetOptionApi
{
    [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;

    public static void RefreshWinInetProxySettings()
    {
        // These lines implement the Interface in the beginning of program 
        // They cause the OS to refresh the settings, causing IP to realy update
        InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0);
        InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);
    }
}

Source : Programmatically Set Browser Proxy Settings in C#

Community
  • 1
  • 1
Rajeesh
  • 391
  • 3
  • 7
  • 1
    It is easier and in fact *simpler* to change the proxy settings via the Windows Registry. You can find out how I did it [here in a program I wrote](https://git.io/vKLsq) which automatically changes the proxy settings based on the network configuration. The essential thing to remember to invoke the [`InternetSetOption`](http://www.pinvoke.net/default.aspx/wininet/internetsetoption.html) method [after making the required changes](https://git.io/vKLsV) to notify other programs of the change. – Alex Essilfie Jul 06 '16 at 13:38
  • You are right Alex, I just checked your code in the link above, already I ended up combining answers from this post as well as the one I mentioned in my answer to get the desired result. Thanks. – Rajeesh Jul 06 '16 at 14:44
1

Better than http://support.microsoft.com/kb/819961, via .REG file, we should refer http://support.microsoft.com/kb/226473 "How to programmatically query and set proxy settings under Internet Explorer", use InternetSetOption().

As http://blogs.msdn.com/b/ieinternals/archive/2013/10/11/web-proxy-configuration-and-ie11-changes.aspx said: "Rather than attempting to “poke” the registry directly, the proper way to update the proxy setting is to use the InternetSetOption API."

Leiyi.China
  • 157
  • 1
  • 4
-2

You just need modify the value:

Registry Key : HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\
DWORD AutoDetect = 0 or 1

See this link.

Alex Essilfie
  • 12,339
  • 9
  • 70
  • 108