如何以编程方式更改LAN设置(代理configuration)

我正在写一个程序来自动切换我的代理地址基于我连接的networking。

到目前为止,除了我在下面强调的部分之外,我已经做了所有的工作。

局域网设置对话框

有没有办法改变自动configuration脚本,并自动检测代码中的设置?

解决scheme可以是P / Invokeregistry编辑。 我只需要一些工作。

您可以通过使用注册表来更改代理设置。 请参阅以下链接:
http://support.microsoft.com/kb/819961

关键路径: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings

价值观:

 "MigrateProxy"=dword:00000001 "ProxyEnable"=dword:00000001 "ProxyHttp1.1"=dword:00000000 "Proxyserver"="http://Proxyservername:80" "ProxyOverride"="<local>" 

SuperUser.com中有关如何禁用自动检测ie代理配置中的设置的问题。 在IE代理配置中禁用“自动检测设置”

从Internet Explorer自动配置脚本定义通过注册表中摘取的代码片段。

脚本1:这将启用AutoConf脚本并定义它是什么(与您的脚本交换http:// xxxx )

  Windows注册表编辑器版本5.00

 [HKEY_CURRENT_USER \ Software \ Microsoft \ Windows \ CurrentVersion \ Internet Settings]
 “AutoConfigURL”= “HTTP://xxx.xxx.xxx.xxx.xxxx”
 “ProxyEnable”= DWORD:00000000

脚本2:此脚本禁用AutoConf脚本并启用具有例外的代理服务器。

 Windows注册表编辑器版本5.00

 [HKEY_CURRENT_USER \ Software \ Microsoft \ Windows \ CurrentVersion \ Internet Settings]
 “ProxyEnable”= DWORD:00000001
 “ProxyOverride”=“proxyexceptionname:端口号; anotherexceptionname:端口
 “访问代理服务器”=“的ftp = MyFTPProxy:端口; HTTP = MYHTTPPROXY:PORT; HTTPS = MYHTTPSPROXY:PORT
 “AutoConfigURL”= “”

我全部搜索了这个。 但是,正如我找不到,我已经写了下面的代码片段,为此目的。

  /// <summary> /// Checks or unchecks the IE Options Connection setting of "Automatically detect Proxy" /// </summary> /// <param name="set">Provide 'true' if you want to check the 'Automatically detect Proxy' check box. To uncheck, pass 'false'</param> public void IEAutoDetectProxy(bool set) { // Setting Proxy information for IE Settings. RegistryKey RegKey = Registry.CurrentUser.OpenSubKey(@"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Connections", true); byte[] defConnection = (byte[])RegKey.GetValue("DefaultConnectionSettings"); byte[] savedLegacySetting = (byte[])RegKey.GetValue("SavedLegacySettings"); if (set) { defConnection[8] = Convert.ToByte(9); savedLegacySetting[8] = Convert.ToByte(9); } else { defConnection[8] = Convert.ToByte(1); savedLegacySetting[8] = Convert.ToByte(1); } RegKey.SetValue("DefaultConnectionSettings", defConnection); RegKey.SetValue("SavedLegacySettings", savedLegacySetting); } 

我正在回答,因为我不允许评论答案。 我想指出操作注册表与使用InternetSetOptionAPI之间的区别。 如果您直接使用注册表来更改代理设置,那么浏览器(如依赖于WinInet代理配置的Chrome)将不会立即获取新设置,但如果使用InternetSetOptionAPI更改,则会立即使用新设置。 这是我的经验。 我没有深入细节,找出在操作注册表之后可以采取什么操作。

编辑:为了刷新WinInet代理设置,你可以做一个简单的PInvoke的InternetSetOption API如下

 internal class InternetSetOptionApi { [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; public static void RefreshWinInetProxySettings() { // These lines implement the Interface in the beginning of program // They cause the OS to refresh the settings, causing IP to realy update InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0); InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0); } } 

来源: 编程设置浏览器代理设置在C#

你只需要修改这个值:

 Registry Key : HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ DWORD AutoDetect = 0 or 1 

看到这个链接 。