1

This is the code that I use to enable the proxy. It has worked fine on all versions of windows up until Server 2008 R2 (Windows 7 Kernal). Now it only works the fist time. I start my application and enable the proxy and that works fine. I then try disabling it using code similar to the code below. It never gets disabled. If I manually disable it by going into internet options and then try enabling it again via my application, it doesn't get enabled.

From what I can tell, the first InternetSetOption command in my code is reverting the changes I make with the regKey.SetValue() function. At least that is what appears to be happening when I step through it. Has anyone seen this problem before, or knows how to fix it. I have been trying to find a work around for awhile now and nothing has turned up. Any input will be appreciated.

regKey.SetValue("ProxyServer", "localhost:" + Settings.Default.Port, RegistryValueKind.String);
regKey.SetValue("ProxyEnable", 1, RegistryValueKind.DWord);
                        regKey.Flush();
InternetSetOption(IntPtr.Zero, 39, IntPtr.Zero, 0); //INTERNET_OPTION_SETTINGS_CHANGED
InternetSetOption(IntPtr.Zero, 37, IntPtr.Zero, 0); //INTERNET_OPTION_REFRESH
Jacob Adams
  • 731
  • 1
  • 10
  • 20

2 Answers2

1

Enabling and disabling the proxy by writing to the registry is not really a good practice. See this question:

Enabling/disabling proxy app doesn't work properly with IE

Community
  • 1
  • 1
MusiGenesis
  • 74,184
  • 40
  • 190
  • 334
0

Although definitely not a clean solution this work-around does work on Server 2008 R2 and Windows 7.

After setting proxy-related registry keys, simply starting Internet Explorer refreshes the SystemWebProxy:

Process objProcess = Process.Start("IEXPLORE.EXE", "-nomerge " + @"http://www.google.com/");
Thread.Sleep(TimeSpan.FromSeconds(2));
objProcess.Kill();
Leslie Davies
  • 4,052
  • 1
  • 16
  • 14