如何使用C#.NET更改“立即生效”的全局Windows代理`
我正在编写一个Winform的(C#.NET)应用程序来更改Windows的全局(又名Internet Explorer)代理设置 我在用这个如何使用C#.NET更改“立即生效”的全局Windows代理`,c#,.net,proxy,C#,.net,Proxy,我正在编写一个Winform的(C#.NET)应用程序来更改Windows的全局(又名Internet Explorer)代理设置 我在用这个 RegistryKey registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true); registry.SetValue("ProxyEnable", 1); registry.Set
RegistryKey registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
registry.SetValue("ProxyEnable", 1);
registry.SetValue("ProxyServer", "127.0.0.1:8080");
但它的行为方式很奇怪。我使用两个浏览器对此进行了测试
- 谷歌浏览器:
互联网选项(inetcpl.cpl)>连接>局域网设置时
。现在考虑之前的代理变更。当我说“开门”时,我的意思是“开门”。我的意思是,不编辑或点击任何其他按钮。我猜,全球代理是真的被改变了(通过从注册表读取)&谷歌浏览器立即生效
- Internet Explorer 8:
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);
调用这段代码来设置代理是可行的,但是从同一个程序中调用重置以前的值(我从注册表中提取并存储的值)的后续调用没有效果。有什么想法吗?@claws代码运行良好。但是如何编写凭证呢?“用户名”和“密码”?这个代码很好。。。它肯定不能在Windows10上运行。它不会更新UI中的代理设置,并且代理本身在更新后不会工作this@NikasŽalias我只使用上面的代码,它在windows 10上工作。IE:请看这一页。