How to change LAN Settings (proxy configuration) programmatically

39,808

Solution 1

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"=""

Solution 2

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);
    }

Solution 3

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#

Solution 4

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."

Share:
39,808

Related videos on Youtube

Alex Essilfie
Author by

Alex Essilfie

I the team leader of a freelance software developing group mainly specialising in Desktop Application development. I taught myself programming in 2005. My primary languages are Visual Basic (VB6 &amp; VB.NET), C# and Python. Contact: email: [email protected] (Solve reCAPTCHA to view email) I am #SOreadytohelp Please start the title of your message with StackOverflow so it gets filtered and I get to you faster.

Updated on July 07, 2020

Comments

  • Alex Essilfie
    Alex Essilfie almost 4 years

    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
    Alex Essilfie about 13 years
    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. 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
    Alex Essilfie over 9 years
    It seems to require quite some P/Invoke. I'll try it out and see how it goes. Thanks.
  • Alex Essilfie
    Alex Essilfie almost 8 years
    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 which automatically changes the proxy settings based on the network configuration. The essential thing to remember to invoke the InternetSetOption method after making the required changes to notify other programs of the change.
  • Rajeesh
    Rajeesh almost 8 years
    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.
  • Ashok
    Ashok over 4 years
    Great help. Thank you for saving my time.

Related