gpt4 book ai didi

c# - 如何使用带有 `Immediate Effect` 的 C# .NET 更改全局 Windows 代理

转载 作者:IT王子 更新时间:2023-10-29 04:43:44 25 4
gpt4 key购买 nike

我正在编写一个 Winform 的 (C# .NET) 应用程序来更改 Windows 的全局(又名 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");

但是它的行为很奇怪。我使用两种浏览器对此进行了测试

  • 谷歌浏览器

当我在 Chrome 运行时更改/禁用代理时。 Chrome 仍在使用以前的代理。更改不会影响其过程。但是当我只是打开 Internet Options(inetcpl.cpl) > Connections > LAN Settings 时。现在考虑以前的代理更改。当我说打开时,我的意思是打开。我的意思是,不编辑或点击任何其他按钮。我想,当时全局代理真的正在发生变化(通过从注册表中读取)并且谷歌浏览器立即生效。

  • Internet Explorer 8:

Internet Explorer 的情况更糟。在 IE 运行时使用我的应用程序更改/禁用代理后,甚至在转到“Internet 选项 (inetcpl.cpl) > 连接 > Lan 设置”后,正在运行的 IE 代理不会受到影响。即使我在新标签页中打开一个新链接也不行。我必须重新启动 IE 才能合并该更改。

我想要的行为是,每当我在我的应用程序中更改代理设置时,所有使用全局代理的浏览器(无论它们是否正在运行)都应该立即将更改合并到设置中.

我怎样才能做到这一点?

最佳答案

The behavior I want is that when ever I change proxy settings in my app, all the browsers which are using global proxy (irrespective of whether they are running or not) should instantly incorporate the change in settings.

How can I achieve this?

您需要刷新系统才能实现这一点。

在代码的开头添加这些行:

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

并隐含代码:

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

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

关于c# - 如何使用带有 `Immediate Effect` 的 C# .NET 更改全局 Windows 代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2020363/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com