[英]How to change LAN Settings (proxy configuration) programmatically
我正在編寫一個程序,根據我連接的網絡自動切換代理地址。
到目前為止,我已經完成了所有工作,除了我在下面重點介紹的部分。
有沒有辦法更改自動配置腳本和自動檢測代碼中的設置?
解決方案可以是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>"
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 Registry Editor Version 5.00 [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings] "ProxyEnable"=dword:00000001 "ProxyOverride"="proxyexceptionname:portnumber;anotherexceptionname:port "ProxyServer"="ftp=MyFTPProxy:Port;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);
}
優於http://support.microsoft.com/kb/819961 ,通過.REG文件,我們應該參考http://support.microsoft.com/kb/226473 “ 如何在Internet Explorer下以編程方式查詢和設置代理設置 ” ,使用InternetSetOption()。
正如http://blogs.msdn.com/b/ieinternals/archive/2013/10/11/web-proxy-configuration-and-ie11-changes.aspx所說:“而不是試圖直接”戳“注冊表,更新代理設置的正確方法是使用InternetSetOption API。“
我正在回答,因為我不允許對答案發表評論。 我想指出操縱注冊表與使用InternetSetOptionAPI之間的區別。 如果您直接戳注冊表以更改代理設置,那么依賴於WinInet代理配置的Chrome等瀏覽器將不會立即獲取新設置,但如果您使用InternetSetOptionAPI進行更改,則會立即使用新設置。 這是我的經歷。 我沒有詳細介紹在操作注冊表后可以采取哪些措施來獲取設置。
編輯:為了刷新WinInet代理設置,您可以執行InternetSetOption API的簡單PInvoke,如下所示
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);
}
}
你只需要修改值:
Registry Key : HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\
DWORD AutoDetect = 0 or 1
看到這個鏈接 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.