Enable/Disable Proxy in IE through C#

First of all i´m going to write this blog entry in English instead of Icelandic in the hope that more people benefit from it.

UPDATE: Secondly you should regard my approach as a registry hack. I did my best to find the programming solution to this but the API information was apparently hard to find so i wrote this article in the hope that somebody would find it and point me to the right solution. That just happend this week when i found that somebody had found my entry too complex and probably too high-level. A reader pointed him to this entry that was written after mine! "Setting Windows internet connection proxy from C#" I would have used that one! Still mine should give you an interesting window to the problem.

Hopefully I will get some interesting feedback on it in the end (Update: I have, see comments). I will update this entry in the next few days hopefully with a C# class doing some proxy "on/of,read state stuff"! Here it is in C# ProxyClass.cs (5.16 kb)

But here we go... The thing is I had a problem (this weekend) changing the proxy settings of my browser via C# code. (Nb. this is a WINDOWS FORM problem but not ASP.net page problem, just so you know) .... but anyway, I ended up finding few methods to do this and I will now try to explain them.

First of all I did not want to record and run a macro that would open up the browser and "walk through" to the proxy settings window and then enable/disable the proxy ( although my end result is something like it). I wanted to do this with programmed code, but maybe not the "whole nine yard" lowelevel code.

The problem: I needed to change the proxy in Internet Explorer from the GUI of my program windows program. In the the program (used for testers)  I show the enable/disable state of the proxy (Green button with the text "Proxy On" and a Red button with the text "Proxy Off" depending on the state of the proxy).

The best article I found about how to do this "old school stuff style" was this one here.  This was the article that got me on the right track.

Here are the steps i went through to find a solution that was not perfect but worked for me.


I knew that I could probably change some values in the registry to turn the proxy on/off. And I was right.

Setting this reg entry to 0 makes the proxy enabled:
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings] "ProxyEnable"=dword:00000000

And setting the same value to 0 disables it ("ProxyEnable"=dword:00000001). See more here about changing registry with bat files

But changing the registry entry did not help me that much because to see it enabled in the browser (or my form) you need to restart the browser. Maybe there is some regex reload command out there? If somebody knows how to do it i would love if you wrote a comment to this blog.


So then what I decided to do was to use System.Diagnostics.Process to just run the "disable proxy command", just like I have done with the delete cookies command i have in my program .

This code opens up the IE browser window that has the delete cookie window. You can run this command if you press the windows key + R and put  InetCpl.cpl ClearMyTracksByProcess 2 in the editbox and press enter.

Process proc = new Process();
proc.StartInfo.FileName = @"C:\WINNT\\system32\\RunDll32.exe";
proc.StartInfo.Arguments = "InetCpl.cpl ClearMyTracksByProcess 2";

So i thought i could just do the same thing with the proxy. Open up the proxy window and uncheck "Use a proxy server for your LAN...".  I found out there were at least three arguments that you can use (please let me know if I´m missing any you know about) to fire up the Internet Properties window.

* rundll32.exe shell32.dll,Control_RunDLL inetcpl.cpl,,4
* rundll32 inetcpl.cpl,LaunchConnectionDialog
* rundll32 shell32.dll,Control_RunDLL inetcpl.cpl,Internet,4

BUT i hit a snag with this aproach. There is no direct disable/enable switch for the proxy at this window. You need to go one window further (the "LAN settings window contains the window with the checkbox i was interested in).


So like we say in Icelandic "Nú voru góð ráð dýr" or "Now Good advices are expensive/animals" (direct translation). I was going to have to navigate somehow to the LAN window.. some "macro thingy" was needed after all.

I googled and found a VB solution. So I whipped the solution to C#:

           Process proc = new Process();
           proc.StartInfo.FileName = @"C:\WINNT\\system32\\RunDll32.exe";
           proc.StartInfo.Arguments = "shell32.dll,Control_RunDLL inetcpl.cpl,Internet,4";
           proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; //The window will not pop up

            //Here we need to pause until the window has opened to be able to send commands to it.
            //Pause for 1/2 second because the window has to be open before we try to open it, else the command just fires into a void!
            //Same as ALT+L


Here are more SendKey Parameters (keyboard pressed keys that is) . NB. I found out that if i send many command at once (etc. SendKeys.SendWait("%L{ENTER}");) it sometimes works and sometimes it doesn´t. Its probably because the second command fires before the first has finnished executing. In this case i found out (with trial and error) that 100 ms sleep between commands worked best.

Here is a C# code that gets the proxy enabled/disabled state from the registry.

RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings");
int proxyStatus = (int)key.GetValue("ProxyEnable");
//proxyStatus == 0 means the proxy is on
//proxyStatus == 1 means the proxy is off

Hopefully this article has helped somebody out there. If so my job is done! :-)

But if somebody has some other brilliant ideas how to enable/disable proxy's without opening up browers I would love to hear from you! Update: I found a interesting entry here, do read the questions and answers on proxies in the comments.

And then here are some discovery texts for you out there that are finding it hard to locate information about some of the things I was doing here.

1. "Changing the proxy in IE programmatically in C#"
2. "Enable proxy through code"
3. "Disable proxy through code"
4. "Tabbing through program windows in code"
5. "Doing bat stuff in code"
6. "Doing macro stuff in code"
7. "Enable proxy in c#"
8. "Change proxy in c#"

I added to this blog entry a C# class (ProxySettings) that you can use to get the registry settings from both Internet Explorer and Firefox. But with the class you can only enable/disable the settings in IE. Doing it in Firefox demands some file,string manipulation that i realy have no time or interest doing! But if somebody does it i would love to add that code to this entry.

Here is the class in C# (nothing beautiful but gets the job done): ProxyClass.cs (5.16 kb)

Currently rated 3.2 by 19 people

  • Currently 3.210526/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by: sturlath
Posted on: 9/22/2008 at 10:47 AM
Tags: ,
Categories: Forritun
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (2) | Post RSSRSS comment feed