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

Joh*_*ler 35 c# registry proxy

我正在编写一个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");
Run Code Online (Sandbox Code Playgroud)

进入注册表是最好的方法,还是有更推荐的方法?如果有替代解决方案,我想避免注册表更改.

JSB*_*ոգչ 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).

  • 据我所知,WinHTPpSetDefaultProxyConfiguration设置的WinHTTP代理设置**与Internet Explorer设置不同**.13个投票的人中有没有人真的尝试*尝试过吗? (7认同)
  • 1.>如果你真的想更新注册表中的代理设置`<如果我们需要更改注册表项,为什么我们需要WinAPI?我们可以在问题中使用C#来做到这一点. (4认同)

小智 21

来自: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;
Run Code Online (Sandbox Code Playgroud)

并暗示代码:

        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);
Run Code Online (Sandbox Code Playgroud)


131*_*131 11

我写了一个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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 是的,可以确认, Registry.SetValue(keyName, "ProxyEnable", proxyEnabled?"1": "0"); 不会工作,但 Registry.SetValue(keyName, "ProxyEnable", proxyEnabled? 1: 0); 工作正常。(值不应作为字符串传递) (3认同)

Jar*_*Par 6

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

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

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