यहां कार्यान्वयन है जिसे मैं .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;
}
}
}
स्रोत
2009-06-10 01:40:59
मुझे नहीं लगता कि आप कर सकते हैं ... लेकिन अगर आप मुझे यह देखना पसंद करेंगे कि कैसे! – marcgg
मुझे नहीं लगता कि आप या तो सुरक्षा कारणों से कर सकते हैं। ऐसा कहकर, उन्हें * कहीं * संग्रहीत किया जाना चाहिए। रजिस्ट्री में माइक्रोसॉफ्ट को जानना ... – Powerlord
@ आर। Bemrose, इस के लिए अनुमति देने के लिए कोई सुरक्षा कारण नहीं है। यदि किसी दुर्भावनापूर्ण प्रोग्राम में HKCU तक पहुंच पहुंच है, तो यह शायद ही कभी सबसे खराब हो सकता है। यदि कोई * वेबसाइट * विश्वसनीय साइटों पर खुद को जोड़ सकती है, तो अब यह बुरा होगा। –