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();
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); }
source share