0

I try to use c# program to change proxy server address.

    [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;

    void SetProxy()
    {
        RegistryKey RegKey = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
        RegKey.SetValue("ProxyServer", "192.168.1.1:8082");
        RegKey.SetValue("ProxyEnable", 1);

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

First thing is I cant find ProxyServer key in registry and when I created it and still it doesn't solve the problem. And when I try to enter manually in my pc and I found that still proxy server is not used by internet explorer. And it shows my own ip in whatismyip.com

I don't understand why it is happening even I tried to reinstall the os.. problem still exists So is there any solution?

Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184
Ajit Hegde
  • 542
  • 1
  • 15
  • 33
  • possible duplicate of [Programmatically Set Browser Proxy Settings in C#](http://stackoverflow.com/questions/197725/programmatically-set-browser-proxy-settings-in-c-sharp) – EricLaw Oct 08 '13 at 21:42

3 Answers3

1

This does it, your welcome. No need to close IE either as it will refresh the session:

public const int INTERNET_OPTION_REFRESH = 37;
    private const int INTERNET_OPTION_END_BROWSER_SESSION = 42;
    public const int INTERNET_OPTION_SETTINGS_CHANGED = 39;

    [DllImport("wininet.dll", SetLastError = true)]
    private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int lpdwBufferLength);


    public void RefreshBrowserSettings()
    {
        InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0);
        InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);
    }

public void SetProxy(String Proxy, String Port, bool enabled = true)
    {
        string proxy = Proxy + ":" + Port;
        string key = "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";
        RegistryKey RegKey = Registry.CurrentUser.OpenSubKey(key, true);

        if (Proxy != "")
        {
            RegKey.SetValue("ProxyServer", proxy);
        }

        if (enabled && Proxy != "")
        {
            RegKey.SetValue("ProxyEnable", 1);
        }
        else
        {
            RegKey.SetValue("ProxyEnable", 0);
        }

        RefreshBrowserSettings();

    }
Aaron Gibson
  • 1,280
  • 1
  • 21
  • 36
0

From msdn...

Regedit4
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings]
"MigrateProxy"=dword:00000001
"ProxyEnable"=dword:00000001
"ProxyHttp1.1"=dword:00000000
"ProxyServer"="http://ProxyServername:80"
"ProxyOverride"=""

but you are setting Proxy server value like; RegKey.SetValue("ProxyServer", "192.168.1.1:8082");

Without "http://"... Did you tried with that ?

Also, make sure internet explorer exe has been already closed ( check taskbar running app list )

And one last thing; Did you set proxy options from anorher browsers ? Does it work correctly?

Lost_In_Library
  • 3,265
  • 6
  • 38
  • 70
0

What OS are you using? 32-bit or 64-bit?

Maybe you have a 64-bit Windows, but you're running a 32-bit program to change the registry? In that case, you might have issues due to Windows having partly separate 32-bit and 64-bit registries.

user9876
  • 10,954
  • 6
  • 44
  • 66