Why can I only change the registry for the first time?

I am trying to write an application that automatically changes the proxy based on an active network connection. In this application, the user can also manually click on the server in the notifyIcon context menu and call the function to switch the server.

My problem is this: the application changes the proxy server the first time the function is called, but after that it won’t work. I have included debug statements to make sure that the correct proxy is passed to this function (and this is really correct), but registry entries never change after the first time. What am I doing wrong?

Here is my proxy class:

class Proxy { [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; static bool settingsReturn, refreshReturn; public void SetProxy(ProxyList proxy) { RegistryKey registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true); registry.SetValue("ProxyEnable", 1); registry.SetValue("ProxyServer", proxy.server + ":" + proxy.port); registry.Close(); // 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); } } 

And this is how I call the function from the context menu:

 void Form1_Click(object sender, EventArgs e) { Proxy proxyServer = new Proxy(); ToolStripMenuItem item = (ToolStripMenuItem)sender; proxyServer.SetProxy(XML.proxy[(int)item.Tag]); proxyServer = null; notifyIcon1.BalloonTipText = XML.proxy[(int)item.Tag].name + " is now your Active Proxy"; notifyIcon1.ShowBalloonTip(1); } 
+4
source share
3 answers

From the Microsoft Knowledge Base: How to program and set proxy server settings in Internet Explorer

Note INTERNET_OPTION_PER_CONNECTION_OPTION leads to a change in the system-wide settings when using the NULL descriptor. To correctly reflect the global proxy server settings, you must call the InternetSetOption function using the INTERNET_OPTION_REFRESH option flag.

This is from MSDN

INTERNET_OPTION_PER_CONNECTION_OPTION
75
Sets or returns an INTERNET_PER_CONN_OPTION_LIST structure that sets a list of options for a specific connection. This is used by InternetQueryOption and InternetSetOption. This parameter is valid only in Internet Explorer 5 and later.

Read about the structure of INTERNET_PER_CONN_OPTION_LIST .

Note: the value for the flag INTERNET_PER_CONN_PROXY_SERVER is 2.

+1
source

The RegistryKey class is one-time. Maybe it is cached or something like that, try to get rid of it in addition to closing it.

0
source

I think you better make a plugin for your browser, instead of directly changing the registry.

-one
source

All Articles