2011-04-06 98 views
7

我正在編寫一個程序,以根據我連接的網絡自動切換我的代理地址。如何以編程方式更改局域網設置(代理配置)

到目前爲止,我已經得到了一切工作,除了我在下面突出顯示的部分。

LAN Settings Dialog

有什麼辦法來改變自動配置腳本,並在代碼中自動檢測設置?

該解決方案可以是P/Invoke註冊表編輯。我只需要一些有用的東西。

回答

14

您可以使用註冊表更改代理設置。請參閱以下鏈接:
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>" 

一個question in SuperUser.com關於如何禁用自動檢測IE代理的配置設置。在IE代理配置中禁用「自動檢測設置」

摘自Internet Explorer Automatic Configuration Script Definition via Registry的摘錄。

腳本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"="" 
+0

感謝在'AutoConfigUrl'的信息。那是我尋找的一件事。通過閱讀[Andrew Swan對您鏈接的SuperUser.com問題的評論],我發現如何禁用/啓用其他複選框(*自動檢測設置*)(http://superuser.com/questions/79703/disable-automatically -detect - 設置 - 中 - 即代理配置/ 79718#評論-217210)。它基本上說從HKEY_CURRENT_USER \ Software \ Microsoft \ Windows \ CurrentVersion \ Internet Settings \ Connections | DefaultConnectionSettings的第九個字節中減去8來禁用該選項。 – 2011-04-06 19:37:11

7

我搜索了這一切。但是,正如我找不到,我已經寫了下面的代碼片段,適用於此目的。

/// <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); 
    } 
1

http://support.microsoft.com/kb/819961更好,通過.reg文件,我們應該參考http://support.microsoft.com/kb/226473How to programmatically query and set proxy settings under Internet Explorer」,使用InternetSetOption()。

As http://blogs.msdn.com/b/ieinternals/archive/2013/10/11/web-proxy-configuration-and-ie11-changes.aspx said:「與其試圖直接」戳「註冊表,更新代理設置的正確方法是使用InternetSetOption API。」

+0

它似乎需要相當多的P/Invoke。我會試試看看它是如何發展的。謝謝。 – 2014-12-29 11:03:58

-1

你只需要修改的值:

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

this link

0

我在回答,因爲我不允許評論答案。我想指出操作註冊表與使用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); 
    } 
} 

來源:​​3210

+1

通過Windows註冊表更改代理設置更簡單實際更簡單。你可以在我寫的程序中找到我是如何做到的(https://git.io/vKLsq),它根據網絡配置自動更改代理設置。記住在[進行所需更改後調用['InternetSetOption'](http://www.pinvoke.net/default.aspx/wininet/internetsetoption.html)方法)(https://git.io/vKLsV)通知其他程序的變化。 – 2016-07-06 13:38:49

+0

你是對的Alex,我剛剛在上面的鏈接中檢查了你的代碼,我已經結合了這篇文章的答案以及我在答案中提到的答案以獲得期望的結果。謝謝。 – Rajeesh 2016-07-06 14:44:15

相關問題