Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change LAN Settings (proxy configuration) programmatically

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.

like image 551
Alex Essilfie Avatar asked Apr 06 '11 16:04

Alex Essilfie


Video Answer


3 Answers

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"=""
like image 126
ichen Avatar answered Oct 12 '22 00:10

ichen


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);
    }
like image 38
Shankar Arigela Avatar answered Oct 12 '22 00:10

Shankar Arigela


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#

like image 2
Rajeesh Avatar answered Oct 12 '22 00:10

Rajeesh