如何使用带有“立即生效”的 C# .NET 更改全局 Windows 代理
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2020363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change Global Windows Proxy using C# .NET with `Immediate Effect`
提问by claws
I'm writing a Winform's (C# .NET) app to change Windows' Global (aka Internet Explorer's) proxy settings.
我正在编写一个 Winform (C# .NET) 应用程序来更改 Windows 的全局(又名 Internet Explorer)代理设置。
I'm using this.
我正在使用这个。
RegistryKey registry = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Internet Settings", true);
registry.SetValue("ProxyEnable", 1);
registry.SetValue("ProxyServer", "127.0.0.1:8080");
But its behaving in a weird manner. I tested this using two browsers
但它的行为方式很奇怪。我使用两个浏览器对此进行了测试
- Google Chrome:
- 谷歌浏览器:
When I change/Disable the proxy while Chrome is running. Chrome is still using the previous proxy. The change is not effecting its process. But when I JUST openInternet Options(inetcpl.cpl) > Connections > LAN Settings
. The previous change of proxy is now considered. When I said Just openI really mean Just open. I mean, not editing or clicking any other buttons. I guess, its then the global proxy is reallygetting changed (by reading from registry) & Google Chrome is immediately taking the effect.
当我在 Chrome 运行时更改/禁用代理时。Chrome 仍在使用以前的代理。更改不会影响其进程。但是当我刚打开Internet Options(inetcpl.cpl) > Connections > LAN Settings
. 现在考虑之前的代理更改。当我说Just open 时,我的意思是Just open。我的意思是,不要编辑或单击任何其他按钮。我想,那时全局代理确实发生了变化(通过从注册表读取)& Google Chrome 立即生效。
- Internet Explorer 8:
- 浏览器 8:
Case with Internet Explorer is much worse. After changing/disabling the proxy using my app while IE is running & Even after going to "Internet Options(inetcpl.cpl) > Connections > Lan Settings" The running IE proxy isn't getting affected. Not even if I open a new link in a new tab. I had to restart IE for that change to be incorporated.
Internet Explorer 的情况要糟糕得多。在 IE 运行时使用我的应用程序更改/禁用代理后,即使在转到“Internet 选项(inetcpl.cpl)> 连接 > Lan 设置”之后,正在运行的 IE 代理也不会受到影响。即使我在新标签页中打开一个新链接也不会。我必须重新启动 IE 才能合并该更改。
The behavior I want is that whenever I change proxy settings in my app, all the browsers which are using global proxy (irrespective of whether they are running or not) should instantlyincorporate the change in settings.
我想要的行为是,每当我更改应用程序中的代理设置时,所有使用全局代理的浏览器(无论它们是否正在运行)都应立即合并设置中的更改。
How can I achieve this?
我怎样才能做到这一点?
采纳答案by claws
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?
我想要的行为是,每当我更改应用程序中的代理设置时,所有使用全局代理的浏览器(无论它们是否正在运行)都应立即合并设置中的更改。
我怎样才能做到这一点?
You need to refresh your system to achieve that.
您需要刷新系统才能实现这一目标。
Add these lines at the beginning of your code:
在代码的开头添加这些行:
using System.Runtime.InteropServices;
using Microsoft.Win32;
Add this in the beginning of your class:
在课程开头添加:
[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;
And imply the code:
并暗示代码:
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);