37

I'm writing an winforms app that needs to set internet explorer's proxy settings and then open a new browser window. At the moment, I'm applying the proxy settings by going into the registry:

RegistryKey registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
registry.SetValue("ProxyEnable", 1);
registry.SetValue("ProxyServer", "127.0.0.1:8080");

Is going into the registry the best way to do this, or is there a more recommended approach? I'd like to avoid registry changes if there's an alternative solution.

John Miller
  • 999
  • 1
  • 9
  • 15

6 Answers6

22

This depends somewhat on your exact needs. If you are writing a C# app and simply want to set the default proxy settings that your app will use, use the class System.Net.GlobalProxySelection (http://msdn.microsoft.com/en-us/library/system.net.globalproxyselection.aspx). You can also set the proxy for any particular connection with System.Net.WebProxy (http://msdn.microsoft.com/en-us/library/system.net.webproxy.aspx).

If you actually want to update the proxy settings in the registry, I believe that you'll need to use P/Invoke to call the WinAPI function WinHttpSetDefaultProxyConfiguration (http://msdn.microsoft.com/en-us/library/aa384113.aspx).

JSBձոգչ
  • 40,684
  • 18
  • 101
  • 169
  • 4
    1. >If you actually want to update the proxy settings `in the registry`< If we need to change the registry entries, why do we need WinAPI? We can do it using C# like in the question. – claws Jan 07 '10 at 08:42
  • 9
    As far as I can tell, the WinHTTP proxy settings that `WinHttpSetDefaultProxyConfiguration` sets are **not** the same as the Internet Explorer settings. Did any of the 13 people who up-voted this actually *try* it? – Jonathon Reinhart Jul 31 '13 at 22:23
21

from: http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/19517edf-8348-438a-a3da-5fbe7a46b61a

Add these lines at the beginning of your code:

using System.Runtime.InteropServices; using Microsoft.Win32;

    [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;
    bool settingsReturn, refreshReturn;

And imply the code:

        RegKey.SetValue("ProxyServer", YOURPROXY);
        RegKey.SetValue("ProxyEnable", 1);

        // 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);
chris
  • 231
  • 2
  • 2
14

I wrote a 10 lines program to do that, feel free to try https://github.com/131/proxytoggle

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.Win32;

namespace ProxyToggle
{

    class Program
    {

        [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 void setProxy(string proxyhost, bool proxyEnabled)
        {
            const string userRoot = "HKEY_CURRENT_USER";
            const string subkey = "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";
            const string keyName = userRoot + "\\" + subkey;

            if(proxyhost.Length != 0)
               Registry.SetValue(keyName, "ProxyServer", proxyhost);
            Registry.SetValue(keyName, "ProxyEnable", proxyEnabled ? "1" : "0", RegistryValueKind.DWord);

            // These lines implement the Interface in the beginning of program 
            // They cause the OS to refresh the settings, causing IP to realy update
            InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0);
            InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);
        }

        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                setProxy("", false);
                return;
            }

            setProxy(args[0], true);
        }
    }
}
Hassan Uddin
  • 417
  • 5
  • 10
131
  • 3,071
  • 31
  • 32
  • 1
    Your code is not working since a number 0 or 1 should be set to the ProxyEnable. You are setting a string and this is not working. I am fixing it – ehh Mar 15 '17 at 10:14
  • 1
    My proxy server is getting set automatically by Group Policy or something. When I change it using your way by just executing the app it works just fine. When I'm trying to do the same from the windows service registry value gets set but immediately reverts to the policy default when I make any of these `InternetSetOption` calls. If I don't call InternetSetOption` - registry value changes but not the actual active proxy. Have no idea why. – rook Apr 12 '17 at 22:14
  • 3
    Yep, can confirm, that Registry.SetValue(keyName, "ProxyEnable", proxyEnabled?"1": "0"); won't work, but Registry.SetValue(keyName, "ProxyEnable", proxyEnabled? 1: 0); works fine. (Values should be passed not as strings) – Vital May 06 '17 at 11:40
6

Check out this KB article specifically tagged at what you're trying to do.

http://support.microsoft.com/kb/226473

The short version is you want to use the InternetOpen, InternetSetOption API's to update the proxy settings.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
3

You can use this useful method existing since FW 2.0: (i've just discovered and i'm another man now...)

http://msdn.microsoft.com/en-us/library/system.net.webrequest.getsystemwebproxy.aspx

-2

Quick Code example (from msdn):

WebProxy proxyObject = new WebProxy("http://proxyserver:80/",true);
WebRequest req = WebRequest.Create("http://www.contoso.com");
req.Proxy = proxyObject;
Dave
  • 12,117
  • 10
  • 46
  • 52