如何使用带有“立即生效"的 C# .NET 更改全局 Windows 代理 [英] How to change Global Windows Proxy using C# .NET with `Immediate Effect`

查看:38
本文介绍了如何使用带有“立即生效"的 C# .NET 更改全局 Windows 代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 Winform (C# .NET) 应用程序来更改 Windows 的全局(又名 Internet Explorer)代理设置.

I'm writing a Winform's (C# .NET) app to change Windows' Global (aka Internet Explorer's) proxy settings.

我正在使用这个.

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

  • 谷歌浏览器:

当我在 Chrome 运行时更改/禁用代理时.Chrome 仍在使用以前的代理.更改不会影响其过程.但是当我只需打开 Internet Options(inetcpl.cpl) >连接 >局域网设置.现在考虑之前的代理更改.当我说打开时,我的意思是打开.我的意思是,不要编辑或单击任何其他按钮.我猜,那时全局代理真的正在改变(通过从注册表读取)&谷歌浏览器立即生效.

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 open Internet Options(inetcpl.cpl) > Connections > LAN Settings. The previous change of proxy is now considered. When I said Just open I really mean Just open. I mean, not editing or clicking any other buttons. I guess, its then the global proxy is really getting changed (by reading from registry) & Google Chrome is immediately taking the effect.

  • Internet Explorer 8:

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

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.

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

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 instantly incorporate the change in settings.

我怎样才能做到这一点?

How can I achieve this?

推荐答案

我想要的行为是,无论何时我在我的应用程序中更改代理设置,全部使用全局的浏览器代理(无论他们是否是否正在运行)应该立即合并设置中的更改.

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;

在课程开头添加:

[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# .NET 更改全局 Windows 代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆