我正在编写一个程序,根据我所连接的网络自动切换代理地址。
到目前为止,我已经让所有东西都可以工作了,除了我下面突出显示的部分。
有没有办法在代码中更改自动配置脚本和自动检测设置?
解决方案可以是P/Invoke注册表编辑。 我只需要能够正常运行的东西。
我正在编写一个程序,根据我所连接的网络自动切换代理地址。
到目前为止,我已经让所有东西都可以工作了,除了我下面突出显示的部分。
有没有办法在代码中更改自动配置脚本和自动检测设置?
解决方案可以是P/Invoke注册表编辑。 我只需要能够正常运行的东西。
您可以通过使用注册表更改代理设置。请参见以下链接:
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>"
关于如何在IE代理配置中禁用自动检测设置的SuperUser.com问题。禁用IE代理配置中的"自动检测设置"
摘自通过注册表定义Internet Explorer自动配置脚本。
脚本1:启用AutoConf脚本并定义脚本内容(将http://xxxx替换为您的脚本内容)
Windows Registry Editor Version 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 /// <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);
}
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#编程方式设置浏览器代理设置
InternetSetOption
方法以通知其他程序进行更改。具体操作请参考此处。 - Alex Essilfie你只需要修改这个值:
Registry Key : HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\
DWORD AutoDetect = 0 or 1
请查看此链接。
AutoConfigUrl
的信息。这正是我在寻找的一件事情。通过阅读你提供的Andrew Swan在SuperUser.com问题中的评论,我找到了如何禁用/启用其他复选框(自动检测设置)。基本上,它说的是从HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections|DefaultConnectionSettings
的第九个字节中减去8来禁用该选项。 - Alex Essilfie