/// <summary> /// Write this value back to the Windows registry /// </summary> /// <param name="registryKey">Parent registry key. Must be open with write permissions.</param> /// <param name="env">Helper class that can map $$-escaped strings.</param> public void WriteToTheRegistry(RegistryKey registryKey, RegEnvReplace env) { string name = Name; if (env != null) { name = env.Map(Name); } RegistryValueKind kind = MapRegis3KindToNativeKind(Kind); if (Value is string) { string stringValue = Value as string; if( (env != null) && stringValue.Contains("$$") ) { stringValue = env.Map(stringValue); if (Kind == RegValueEntryKind.DWord) { stringValue = stringValue.Trim(); if (stringValue.StartsWith("0x", StringComparison.OrdinalIgnoreCase)) { registryKey.SetValue(name, int.Parse(stringValue.Substring(2), System.Globalization.NumberStyles.HexNumber), kind); } else { registryKey.SetValue(name, int.Parse(stringValue), kind); } } else if (Kind == RegValueEntryKind.QWord) { registryKey.SetValue(name, long.Parse(stringValue), kind); } else { registryKey.SetValue(name, stringValue, kind); } } else { registryKey.SetValue(name, stringValue, kind); } } else { registryKey.SetValue(name, Value, kind); } }
/// <summary> /// Write the contents of this object back to the registry (possibly recursively) /// </summary> /// <param name="registryWriteOptions">Options for writing to the registry</param> /// <param name="env">Optional handler for environment variable replacement</param> /// <param name="registryView">Type of registry you want to see (32-bit, 64-bit, default).</param> public void WriteToTheRegistry(RegistryWriteOptions registryWriteOptions, RegEnvReplace env, RegistryView registryView) { if ((registryWriteOptions & RegistryWriteOptions.Recursive) != 0) { foreach (RegKeyEntry subkey in Keys.Values) { subkey.WriteToTheRegistry(registryWriteOptions, env, registryView); } } string rootPath = env.Map(Path); string rootPathWithoutHive; using (RegistryKey registryKey = Regis3.OpenRegistryHive(rootPath, out rootPathWithoutHive, registryView)) { if ((registryWriteOptions & RegistryWriteOptions.AllAccessForEveryone) != 0) { using (RegistryKey subkey = registryKey.CreateSubKey(rootPathWithoutHive, RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryOptions.None, AllAccessForEveryone)) { foreach (RegValueEntry regValueEntry in Values.Values) { regValueEntry.WriteToTheRegistry(subkey, env); } subkey.Close(); } } else { using (RegistryKey subkey = registryKey.CreateSubKey(rootPathWithoutHive)) { foreach (RegValueEntry regValueEntry in Values.Values) { regValueEntry.WriteToTheRegistry(subkey, env); } subkey.Close(); } } } }