以编程方式在C#中设置浏览器代理设置

时间:2008-10-13 14:17:41

标签: c# proxy registry

我正在编写一个winforms应用程序,需要设置Internet Explorer的代理设置,然后打开一个新的浏览器窗口。目前,我正在通过进入注册表来应用代理设置:

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

进入注册表的最佳方式是做到这一点,还是有更推荐的方法?如果有替代解决方案,我想避免注册表更改。

6 个答案:

答案 0 :(得分:21)

这在某种程度上取决于您的确切需求。如果您正在编写C#应用程序并且只想设置应用程序将使用的默认代理设置,请使用System.Net.GlobalProxySelection类(http://msdn.microsoft.com/en-us/library/system.net.globalproxyselection.aspx)。您还可以使用System.Net.WebProxy(http://msdn.microsoft.com/en-us/library/system.net.webproxy.aspx)为任何特定连接设置代理。

如果您确实想要更新注册表中的代理设置,我相信您需要使用P / Invoke来调用WinAPI函数WinHttpSetDefaultProxyConfiguration(http://msdn.microsoft.com/en-us/library/aa384113.aspx)。

答案 1 :(得分:19)

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

在代码的开头添加以下行:

使用System.Runtime.InteropServices; 使用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;

并暗示代码:

        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);

答案 2 :(得分:10)

我写了一个10行程序,可以试试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;

            Registry.SetValue(keyName, "ProxyServer", proxyhost);
            Registry.SetValue(keyName, "ProxyEnable", proxyEnabled?"1": "0");

            // 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);
        }
    }
}

答案 3 :(得分:6)

查看此KB文章,专门标记您正在尝试执行的操作。

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

简短版本是您要使用InternetOpen,InternetSetOption API来更新代理设置。

答案 4 :(得分:3)

您可以使用自FW 2.0以来存在的这种有用方法: (我刚刚发现,我现在是另一个男人......)

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

答案 5 :(得分:-2)

快速代码示例(来自msdn):

WebProxy proxyObject = new WebProxy("http://proxyserver:80/",true);
WebRequest req = WebRequest.Create("http://www.contoso.com");
req.Proxy = proxyObject;
相关问题