/// <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 = 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(); } } } }
private int HandleSingleFile() { RegKeyEntry regKeyEntry = Files[0]; if (!string.IsNullOrEmpty(MergeFile)) { Console.WriteLine("Writing {0}...", MergeFile); RegFileExportOptions options = RegFileExportOptions.None; if (NoEmptyKeys) options |= RegFileExportOptions.NoEmptyKeys; CreateRegFileExporter(MergeFile).Export(regKeyEntry, MergeFile, options); Console.WriteLine(); } if (WriteToTheRegistry) { RegEnvReplace env = new RegEnvReplace(); if (!string.IsNullOrEmpty(ParamsFilename)) { if (!File.Exists(ParamsFilename)) { Console.WriteLine("ERROR, /params file '{0}' not found", ParamsFilename); return 10; } try { if (ParamsFilename.EndsWith(".ini", StringComparison.OrdinalIgnoreCase)) { env.ReadIniFile(ParamsFilename); } else if (ParamsFilename.EndsWith(".xml", StringComparison.OrdinalIgnoreCase)) { env.ReadXmlFile(ParamsFilename); } else { Console.WriteLine("ERROR, /params file '{0}' has an unsupported extension (only '.xml' and '.ini' are allowed)", ParamsFilename); return 10; } } catch (Exception e) { Console.WriteLine("ERROR, unable to read /params file '{0}'", ParamsFilename); Console.WriteLine(e.Message); return 10; } } env.MergeEnvironmentVariables(); RegistryWriteOptions registryWriteOptions = RegistryWriteOptions.Recursive; if (AllAccess) registryWriteOptions |= RegistryWriteOptions.AllAccessForEveryone; regKeyEntry.WriteToTheRegistry(registryWriteOptions, env, CurrentRegistryView); } else if (AllAccess) { Console.WriteLine("ERROR, /allaccess is only allowed in combination with /write"); return 10; } else if( !string.IsNullOrEmpty(ParamsFilename) ) { Console.WriteLine("ERROR, /params is only allowed in combination with /write"); return 10; } return 0; }
/// <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 = 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); } }