Command Line Proxy Tool with C# Source

One of the post popular ways to set Windows proxy is either via Group Policy or a script. Often these scripts apply the proxy via a registry change. However if you apply via registry change it can be difficult to get the proxy change to instantly take effect in applications, such as Internet Explorer.

Welcome our friend, InternetSetOption in WinInet.dll. Since Windows 2000 this has provided us a mechanism to set the proxy server, and also perform a “refresh” so the affect is instantly applied, with no need to refresh/restart a browser.

InternetSetOption is documented here:

For those in managed code land it can be a pain to get working exactly right, so here I provide a library that allows configuring of all proxy options available via the Internet Explorer GUI.

I’ve wrapped the library into a fully functioning command line program. (Well I hope fully functioning…)

The command line program supports the following parameters:

/proxy:[enable|disable] – Enable/Disable Proxy
/address:proxy:port – Specify Proxy Address
/pac:url – Specify PAC File URL
/autodetect:[enable|disable] – enable/disable auto detect
/exceptions:[exception list]
/connection:[connection name] – specify a dial up connection name to set proxy for
/bypasslocal – Bypass proxy for local connections.

All parameters are optional and you can use in any combination (although that may not always result in usable proxy configuration..)

So for example to enable a proxy with exceptions list:

SetProxy /proxy:enable /address:myproxy:8080 /exceptions:myintranet;*.priv;blahblah /bypasslocal

To then disable proxy:

SetProxy /proxy:disable

Proxy settings should take affect immediately, with no need to relaunch applications such as Internet Explorer.

You can use quotes if parameter requires spaces or if you use <local> in your exception list:

SetProxy /proxy:enable /address:myproxy:8080 /exceptions:”<local>” /connection:”My 3G Connection”

Download the Visual Studio 2010 Project and compiled .NET 4.0 EXE here:!540&authkey=!AJc9O90scGC_epI

About chentiangemalc

specializes in end-user computing technologies. disclaimer 1) use at your own risk. test any solution in your environment. if you do not understand the impact/consequences of what you're doing please stop, and ask advice from somebody who does. 2) views are my own at the time of posting and do not necessarily represent my current view or the view of my employer and family members/relatives. 3) over the years Microsoft/Citrix/VMWare have given me a few free shirts, pens, paper notebooks/etc. despite these gifts i will try to remain unbiased.
This entry was posted in Command Line, Win32 and tagged . Bookmark the permalink.

13 Responses to Command Line Proxy Tool with C# Source

  1. ericlaw1979 says:


    case “/connection”:
    ConnqqqqqqqqectionName = value;

  2. ericlaw1979 says:

    // get count of number of options we need…starting with a base of 2.
    int optionCount = 1;

  3. ericlaw1979 says:

    (Sorry for the spam here)
    I think you also typically want to OR in PROXY_TYPE_DIRECT when setting PROXY_TYPE_PROXY; e.g. m_Int = (int)PerConnFlags.PROXY_TYPE_PROXY | PROXY_TYPE_DIRECT. It’s not clear why you made AutoConfig URLs mutually exclusive with a fixed proxy, but didn’t do the same for AutoProxy?

    If returnValue > 0, you throw, but the function then does a bool return on returnValue < 0 ?

    • no that’s fine, i’m happy for feedback to make this better. it already took me some time just to figure get it working in 1st place ;) of course i cannot remember why i did what i did? not sleeping?i need to check that section more carefully, thanks!

  4. Roberto says:

    is there a way to print out the current proxy setting?

  5. Thomas says:

    Great, only i cant use this in my PE

  6. Marco says:

    Is there an alternate download location? the download server does not resolve anymore,,,

  7. Marco says:

    In this case, I want to offer my sincere condolences on your recent loss. And thanks for offering to re-upload.

  8. Alan Seedhouse says:

    I have been using the SetProxy code to dynamically set the IE proxy settings. The code allocates memory at various points and this appears to be cleared in the Dispose() methods of the structs. Your sample code does not appear to call these methods anywhere so I am assuming that you are relying on the fact the process is short lived. If I used the class in a long running application should I call the Dispose() methods be called manually?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s