Dot-Net

以程式方式將受信任的站點添加到 Internet Explorer

  • December 8, 2014

我正在使用 WatiN 做一個 IE 自動化項目。

點擊要下載的文件時,我在 Internet Explorer 資訊欄中得到以下資訊:

為了幫助保護您的安全,Internet Explorer 已阻止此站點將文件下載到您的電腦。

為了下載報告,我可以手動將站點添加到 Internet Explorer 的受信任站點列表中,但我更願意在 .NET 中以程式方式檢查該站點是否受信任,如果不是,則將其添加到列表中。

僅供參考,我目前正在使用 IE7。

看看這個

基本上看起來你所要做的就是在

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains\DOMAINNAME

然後是一個名為“http”的 REG_DWORD 值,其 value==2

這是我想出的在.NET 中編寫系統資料庫項的實現。

謝謝你讓我朝著正確的方向前進,本。

using System;
using System.Collections.Generic;
using Microsoft.Win32;


namespace ReportManagement
{
   class ReportDownloader
   {
       [STAThread]
       static void Main(string[] args)
       {

           const string domainsKeyLocation = @"Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains";
           const string domain = @"newsite.com";
           const int trustedSiteZone = 0x2;

           var subdomains = new Dictionary<string, string>
                                {
                                    {"www", "https"},
                                    {"www", "http"},
                                    {"blog", "https"},
                                    {"blog", "http"}
                                };

           RegistryKey currentUserKey = Registry.CurrentUser;

           currentUserKey.GetOrCreateSubKey(domainsKeyLocation, domain, false);

           foreach (var subdomain in subdomains)
           {
               CreateSubdomainKeyAndValue(currentUserKey, domainsKeyLocation, domain, subdomain, trustedSiteZone);
           }

           //automation code
       }

       private static void CreateSubdomainKeyAndValue(RegistryKey currentUserKey, string domainsKeyLocation, 
           string domain, KeyValuePair<string, string> subdomain, int zone)
       {
           RegistryKey subdomainRegistryKey = currentUserKey.GetOrCreateSubKey(
               string.Format(@"{0}\{1}", domainsKeyLocation, domain), 
               subdomain.Key, true);

           object objSubDomainValue = subdomainRegistryKey.GetValue(subdomain.Value);

           if (objSubDomainValue == null || Convert.ToInt32(objSubDomainValue) != zone)
           {
               subdomainRegistryKey.SetValue(subdomain.Value, zone, RegistryValueKind.DWord);
           }
       }
   }

   public static class RegistryKeyExtensionMethods
   {
       public static RegistryKey GetOrCreateSubKey(this RegistryKey registryKey, string parentKeyLocation, 
           string key, bool writable)
       {
           string keyLocation = string.Format(@"{0}\{1}", parentKeyLocation, key);

           RegistryKey foundRegistryKey = registryKey.OpenSubKey(keyLocation, writable);

           return foundRegistryKey ?? registryKey.CreateSubKey(parentKeyLocation, key);
       }

       public static RegistryKey CreateSubKey(this RegistryKey registryKey, string parentKeyLocation, string key)
       {
           RegistryKey parentKey = registryKey.OpenSubKey(parentKeyLocation, true); //must be writable == true
           if (parentKey == null) { throw new NullReferenceException(string.Format("Missing parent key: {0}", parentKeyLocation)); }

           RegistryKey createdKey = parentKey.CreateSubKey(key);
           if (createdKey == null) { throw new Exception(string.Format("Key not created: {0}", key)); }

           return createdKey;
       }
   }
}

引用自:https://stackoverflow.com/questions/972345