如何以编程方式更改 LAN 设置(代理配置)

发布于 2024-10-31 02:56:48 字数 225 浏览 2 评论 0原文

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

到目前为止,除了我在下面突出显示的部分之外,我已经完成了所有工作。

LAN 设置对话框

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

解决方案可以是 P/Invoke 注册表编辑。我只需要一些有用的东西。

I am writing a program to automatically switch my proxy address based on the network I am connected to.

I have so far got everything to work except the part that I have highlighted below.

LAN Settings Dialog

Is there any way to change the automatic configuration script and the automatically detect settings in code?

The solution can be either P/Invoke registry editing. I just need something that works.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

烟花易冷人易散 2024-11-07 02:56:48

您可以使用注册表更改代理设置。请参阅以下链接:
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>"

A SuperUser.com 中的问题 有关如何禁用 ie 代理配置中的自动检测设置。在 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 Registry Editor Version 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"=""

You can change proxy settings by using the registry. See the following link:
http://support.microsoft.com/kb/819961

Key path: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings

Values:

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

A question in SuperUser.com regarding how to disable automatically detect settings in ie proxy configuration. Disable "Automatically detect settings" in IE proxy configuration

A snippet, taken from Internet Explorer Automatic Configuration Script Definition via Registry.

Script 1: This enables the AutoConf Script and defines what it is (exchange the http://xxxx with your script)

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

Script 2: This script Disables the AutoConf Script and enables a proxy server with exceptions.

Windows Registry Editor Version 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"=""
迷乱花海 2024-11-07 02:56:48

我彻底搜索了这个。但由于我找不到,我编写了以下适用于此目的的代码片段。

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

I searched all through for this. But as I couldnt find, I had written the below code snippete that works for this purpose.

    /// <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);
    }
并安 2024-11-07 02:56:48

我回答是因为我无权对答案发表评论。我想指出操作注册表与使用 InternetSetOptionAPI 之间的区别。如果您直接点击注册表来更改代理设置,则 Chrome 等依赖于 WinInet 代理配置的浏览器不会立即获取新设置,但如果您使用 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# 中以编程方式设置浏览器代理设置

I am answering because I am not allowed to comment on answers. I would like to point out a difference between manipulating registry vs using InternetSetOptionAPI. If you directly poke registry to change proxy settings then browsers like Chrome that depends on WinInet proxy configuration won't immediately pickup the new settings but if you change using InternetSetOptionAPI the new settings will be used immediately. This is my experience. I did not go into the details to find out what can be done to pickup the settings after manipulating the registry.

EDIT:
In order to refresh the WinInet proxy settings you can do a simple PInvoke of InternetSetOption API as follows

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

Source : Programmatically Set Browser Proxy Settings in C#

渡你暖光 2024-11-07 02:56:48

http://support.microsoft.com/kb/819961 更好,通过 .REG 文件,我们应该参考http://support.microsoft.com/kb/226473如何在 Internet Explorer 下以编程方式查询和设置代理设置”,使用 InternetSetOption()。

作为 http: //blogs.msdn.com/b/ieinternals/archive/2013/10/11/web-proxy-configuration-and-ie11-changes.aspx 说:“而不是试图直接‘戳’注册表,更新代理设置的正确方法是使用 InternetSetOption API。”

Better than http://support.microsoft.com/kb/819961, via .REG file, we should refer http://support.microsoft.com/kb/226473 "How to programmatically query and set proxy settings under Internet Explorer", use InternetSetOption().

As http://blogs.msdn.com/b/ieinternals/archive/2013/10/11/web-proxy-configuration-and-ie11-changes.aspx said: "Rather than attempting to “poke” the registry directly, the proper way to update the proxy setting is to use the InternetSetOption API."

第几種人 2024-11-07 02:56:48

您只需修改该值:

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

请参阅此链接

You just need modify the value:

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

See this link.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文