/// <summary> /// Deletes the value from the Registry. /// </summary> private static void UnregisterValue([NotNull] RegistryValueXml value, [NotNull] IDictionary <string, string> macros) { if (value == null) { throw new ArgumentNullException("value"); } if (macros == null) { throw new ArgumentNullException("macros"); } try { string sKeyPath = SubstituteMacros(macros, value.Key); RegistryKey regkeyRoot = GetWindowsRegistryRootKey(value.Hive); using (RegistryKey regkey = regkeyRoot.OpenSubKey(sKeyPath, true)) { if (regkey == null) { return; // No key, no value to delete } string sName = SubstituteMacros(macros, value.Name); regkey.DeleteValue(sName, false); } } catch (Exception ex) { throw new InvalidOperationException(string.Format("Failed to process the value {0}. {1}", value, ex.Message), ex); } }
/// <summary> /// Writes the value to the Registry. /// </summary> private static void RegisterValue([NotNull] RegistryValueXml value, [NotNull] IDictionary <string, string> macros) { if (value == null) { throw new ArgumentNullException("value"); } if (macros == null) { throw new ArgumentNullException("macros"); } try { string sPath = SubstituteMacros(macros, value.Key); RegistryKey regkeyRoot = GetWindowsRegistryRootKey(value.Hive); RegistryKey regkey = regkeyRoot.OpenSubKey(sPath, true); if (regkey == null) { regkey = regkeyRoot.CreateSubKey(sPath); // TODO: check if this works when more than one level is missing } using (regkey) { string sName = SubstituteMacros(macros, value.Name); object oValue; switch (value.Type) { case RegistryValueTypeXml.Dword: oValue = unchecked ((int)(uint)long.Parse(value.Value)); // Ensure it qualifies as an Int32 for the DWORD registry value break; case RegistryValueTypeXml.String: oValue = SubstituteMacros(macros, value.Value); break; default: throw new InvalidOperationException(string.Format("The registry value type “{0}” is unknown.", value.Type)); } regkey.SetValue(sName, oValue); } } catch (Exception ex) { throw new InvalidOperationException(string.Format("Failed to process the value {0}. {1}", value, ex.Message), ex); } }