将站点添加到 IE 中的受信任列表(6 以上的所有版本)
程序在
var subdomain = new Dictionary
处崩溃Visual Studio 说的消息是“参数异常未处理”
我不确定如何修复它或它意味着什么?任何帮助将不胜感激。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Win32; namespace LotusTrustedSites { 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 { {"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 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; } } }
The program crashes at
var subdomain = new Dictionary
The message that Visual Studio says is "Argument exception was unhandled"
I'm not sure how to fix it or what it means? Any help would be appreciated.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Win32; namespace LotusTrustedSites { 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 { {"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 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; } } }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您有重复的钥匙。
字典
要求键是唯一的。You have duplicate keys.
Dictionary
requires keys to be unique.