Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用C#.NET更改“立即生效”的全局Windows代理`_C#_.net_Proxy - Fatal编程技术网

如何使用C#.NET更改“立即生效”的全局Windows代理`

如何使用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

我正在编写一个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仍在使用以前的代理。这一变化并没有影响其进程。但是当我打开
互联网选项(inetcpl.cpl)>连接>局域网设置时
。现在考虑之前的代理变更。当我说“开门”时,我的意思是“开门”。我的意思是,不编辑或点击任何其他按钮。我猜,全球代理是真的被改变了(通过从注册表读取)&谷歌浏览器立即生效

  • Internet Explorer 8:
Internet Explorer的情况更糟。在IE运行时使用我的应用程序更改/禁用代理后&即使在进入“Internet选项(inetcpl.cpl)>连接>Lan设置”后,运行的IE代理也不会受到影响。即使我在新选项卡中打开新链接也不行。我必须重新启动IE,才能将更改合并

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

我怎样才能做到这一点

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

我怎样才能做到这一点

您需要刷新系统以实现这一点

在代码开头添加以下行:

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:请看这一页。