如何通过程序更改局域网设置(代理配置)

9

我正在编写一个程序,根据我所连接的网络自动切换代理地址。

到目前为止,我已经让所有东西都可以工作了,除了我下面突出显示的部分。

LAN Settings Dialog

有没有办法在代码中更改自动配置脚本和自动检测设置?

解决方案可以是P/Invoke注册表编辑。 我只需要能够正常运行的东西。

5个回答

20

您可以通过使用注册表更改代理设置。请参见以下链接:
http://support.microsoft.com/kb/819961

密钥路径:HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings

值:

"MigrateProxy"=dword:00000001
"ProxyEnable"=dword:00000001
"ProxyHttp1.1"=dword:00000000
"ProxyServer"="http://ProxyServername:80"
"ProxyOverride"="<local>"

关于如何在IE代理配置中禁用自动检测设置的SuperUser.com问题。禁用IE代理配置中的"自动检测设置"

摘自通过注册表定义Internet Explorer自动配置脚本

脚本1:启用AutoConf脚本并定义脚本内容(将http://xxxx替换为您的脚本内容)

Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings] "AutoConfigURL"="http://xxx.xxx.xxx.xxx.xxxx" "ProxyEnable"=dword:00000000

脚本2:此脚本禁用AutoConf脚本并启用带有例外的代理服务器。

Windows注册表编辑器版本5.00
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings] "ProxyEnable"=dword:00000001 "ProxyOverride"="proxyexceptionname:portnumber;anotherexceptionname:port" "ProxyServer"="ftp=MyFTPProxy:Port;http=MYHTTPPROXY:PORT;https=MYHTTPSPROXY:PORT" "AutoConfigURL"=""

感谢关于AutoConfigUrl的信息。这正是我在寻找的一件事情。通过阅读你提供的Andrew Swan在SuperUser.com问题中的评论,我找到了如何禁用/启用其他复选框(自动检测设置)。基本上,它说的是从HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections|DefaultConnectionSettings的第九个字节中减去8来禁用该选项。 - Alex Essilfie

8
我为此进行了全面搜索,但未找到相关内容,因此我编写了以下代码片段以实现此目的。
    /// <summary>
    /// Checks or unchecks the IE Options Connection setting of "Automatically detect Proxy"
    /// </summary>
    /// <param name="set">Provide 'true' if you want to check the 'Automatically detect Proxy' check box. To uncheck, pass 'false'</param>
    public void IEAutoDetectProxy(bool set)
    {
        // Setting Proxy information for IE Settings.
        RegistryKey RegKey = Registry.CurrentUser.OpenSubKey(@"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Connections", true);
        byte[] defConnection = (byte[])RegKey.GetValue("DefaultConnectionSettings");
        byte[] savedLegacySetting = (byte[])RegKey.GetValue("SavedLegacySettings");
        if (set)
        {
            defConnection[8] = Convert.ToByte(9);
            savedLegacySetting[8] = Convert.ToByte(9);
        }
        else
        {
            defConnection[8] = Convert.ToByte(1);
            savedLegacySetting[8] = Convert.ToByte(1);
        }
        RegKey.SetValue("DefaultConnectionSettings", defConnection);
        RegKey.SetValue("SavedLegacySettings", savedLegacySetting);
    }

非常感谢您的帮助,您节省了我的时间。 - Ashok

2
我回答是因为我不能对答案发表评论。我想指出操作注册表与使用InternetSetOptionAPI之间的区别。如果您直接通过poke注册表来更改代理设置,那么依赖于WinInet代理配置的Chrome等浏览器不会立即获取新的设置,但如果您使用InternetSetOptionAPI进行更改,则新的设置将立即生效。这是我的经验。我没有详细了解过在操作注册表后如何获取设置。
编辑: 为了刷新WinInet代理设置,您可以简单地调用InternetSetOption API PInvoke,方法如下:
internal class InternetSetOptionApi
{
    [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;

    public static void RefreshWinInetProxySettings()
    {
        // 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);
    }
}

来源: 以C#编程方式设置浏览器代理设置


1
通过Windows注册表更改代理设置实际上更容易且更简单。您可以在我编写的程序中找到如何操作,该程序可以根据网络配置自动更改代理设置。需要记住的关键是,在进行所需更改后调用InternetSetOption方法以通知其他程序进行更改。具体操作请参考此处 - Alex Essilfie
你说得对,Alex。我刚刚查看了你在上面链接中的代码,最终我结合了这篇帖子以及我在回答中提到的那篇文章的答案来获得所需的结果。谢谢。 - Rajeesh

1

似乎需要相当多的P/Invoke。我会尝试一下,看看情况如何。谢谢。 - Alex Essilfie

-2

你只需要修改这个值:

Registry Key : HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\
DWORD AutoDetect = 0 or 1

请查看此链接


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接