如何使用C#.NET与`立即Effect`改变全球Windows代理 [英] How to change Global Windows Proxy using C# .NET with `Immediate Effect`

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

问题描述

我在写一个WinForm的(C#.NET)应用程序更改Windows的全球(又名Internet Explorer的)代理设置。

我用这个。

 的RegistryKey注册表= Registry.CurrentUser.OpenSubKey(SOFTWARE \\ \\微软的Windows \\ \\ CURRENTVERSION Internet设置,真正的);
registry.SetValue(ProxyEnable,1);
registry.SetValue(访问代理服务器,127.0.0.1:8080);
 

但它在一个奇怪的方式表现。我测试了使用两个浏览器

  • 谷歌浏览器:

当我改变/ Chrome浏览器时运行禁用代理。 Chrome浏览器依然采用了previous代理。这种变化不影响其进程。但是,当我的只需打开 Internet选项(inetcpl.cpl)>连接>局域网设置。代理的previous改变目前被认为。当我说的只需打开的我真正的意思的只需打开的。我的意思是,没有编辑或点击任何其他按钮。我想,它的那么全球代理的真的得到改变(从注册表中读取)及谷歌浏览器立即服用的效果。

  • Internet Explorer 8的:

案例与Internet Explorer是差很多。在改变/用我的应用程序禁用代理,而IE浏览器运行和放大器;即使将Internet选项(inetcpl.cpl)>连接>局域网设置正在运行的IE代理服务器是没有得到受到影响。甚至没有,如果我打开一个新标签的新链接。我不得不重新启动IE浏览器的这种变化被纳入。

我想要的行为是,每当我改变我的应用程序代理设置,所有这一切都是使用全局代理(不论他们是否正在运行与否)的浏览器应的马上的结合中设置的变化

我怎样才能做到这一点?

解决方案
  

我想要的行为是当过   我在我的应用程序更改代理服务器设置,所有   其中使用的是全局的浏览器   代理(不论他们是否   正在运行或没有)应该立即   纳入设置的变化。

     

我怎样才能做到这一点?

您需要刷新系统,以实现这一目标。

将这两行添加在你的code开头:

 使用了System.Runtime.InteropServices;
使用的Microsoft.Win32;
 

添加这在类的开头:

  [的DllImport(的wininet.dll)
公共静态的extern BOOL InternetSetOption(IntPtr的HINTERNET,INT dwOption,IntPtr的lpBuffer,INT dwBufferLength);
公共const int的INTERNET_OPTION_SETTINGS_CHANGED = 39;
公共const int的INTERNET_OPTION_REFRESH = 37;
静态布尔settingsReturn,refreshReturn;
 

和暗示code:

 的RegistryKey注册表= Registry.CurrentUser.OpenSubKey(SOFTWARE \\ \\微软的Windows \\ \\ CURRENTVERSION Internet设置,真正的);
registry.SetValue(ProxyEnable,1);
registry.SetValue(访问代理服务器,YOURPROXY);

//这些行实现程序的开始界面
//它们会导致操作系统刷新使设置,使IP来真的更新
settingsReturn = InternetSetOption(IntPtr.Zero,INTERNET_OPTION_SETTINGS_CHANGED,IntPtr.Zero,0);
refreshReturn = InternetSetOption(IntPtr.Zero,INTERNET_OPTION_REFRESH,IntPtr.Zero,0);
 

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

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 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:

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;

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

这篇关于如何使用C#.NET与`立即Effect`改变全球Windows代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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