/// <summary> /// Creates a new WiX Component for populating with the values of a specific hive, or reuses an existing one. /// Registry items from different hives must not be mixed within a single component. /// </summary> /// <param name="hive">Registry hive.</param> /// <param name="directory">A WiX directory to parent the newly-created component.</param> /// <param name="componentgroup">A group of components to register the newly-created component into.</param> protected Component GetComponentForHive(RegistryHiveXml hive, DirectoryRef directory, ComponentGroup componentgroup) { if (directory == null) { throw new ArgumentNullException("directory"); } if (componentgroup == null) { throw new ArgumentNullException("componentgroup"); } Component component; if (myMapHiveToComponent.TryGetValue(hive, out component)) // Lookup { return(component); // Reuse existing } // Create a new one component = new Component(); myMapHiveToComponent.Add(hive, component); directory.AddChild(component); component.Id = string.Format("{0}.{1}", ComponentIdPrefix, hive); // Chose a GUID for the component, assign component.Guid = myGuidCache[GetGuidId(hive)].ToString("B").ToUpperInvariant(); // Register in the group var componentref = new ComponentRef(); componentgroup.AddChild(componentref); componentref.Id = component.Id; return(component); }
/// <summary> /// Creates a <see cref="RegistryValueXml"/> object. /// </summary> /// <param name="hive">Hive.</param> /// <param name="key">Path to the key under the hive.</param> /// <param name="name">Name of the value.</param> /// <param name="value">Value of the value, must be either a <see cref="string"/> or an <see cref="int"/>.</param> public RegistryValueXml(RegistryHiveXml hive, [NotNull] string key, [NotNull] string name, [NotNull] object value) { if (key == null) { throw new ArgumentNullException("key"); } if (name == null) { throw new ArgumentNullException("name"); } if (value == null) { throw new ArgumentNullException("value"); } Hive = hive; Key = key; Name = name; if (value is string) { Type = RegistryValueTypeXml.String; Value = (string)value; } else if (value is int) { Type = RegistryValueTypeXml.Dword; Value = value.ToString(); } else { throw new InvalidOperationException(string.Format("The value type “{0}” is not supported.", value.GetType().AssemblyQualifiedName)); } }
/// <summary> /// Creates a <see cref="RegistryValueXml"/> object. /// </summary> /// <param name="hive">Hive.</param> /// <param name="key">Path to the key under the hive.</param> public RegistryKeyXml(RegistryHiveXml hive, [NotNull] string key) { if (key == null) { throw new ArgumentNullException("key"); } Hive = hive; Key = key; }
/// <summary> /// Gets the proper Windows Registry root key. /// </summary> private static RegistryKey GetWindowsRegistryRootKey(RegistryHiveXml hive) { switch (hive) { case RegistryHiveXml.Hkcr: return(Registry.ClassesRoot); case RegistryHiveXml.Hklm: return(Registry.LocalMachine); case RegistryHiveXml.Hkcu: return(Registry.CurrentUser); case RegistryHiveXml.Hkmu: return(Registry.LocalMachine); // TODO: differentiate per-machine and per-user, if ever needed default: throw new ArgumentOutOfRangeException("hive", hive, "Unexpected Registry hive."); } }
/// <summary> /// Converts the hive into a guid cache ID. /// </summary> protected static GuidIdXml GetGuidId(RegistryHiveXml hive) { switch (hive) { case RegistryHiveXml.Hkcr: return(GuidIdXml.MsiComponent_RegistryData_Hkcr); case RegistryHiveXml.Hklm: return(GuidIdXml.MsiComponent_RegistryData_Hklm); case RegistryHiveXml.Hkcu: return(GuidIdXml.MsiComponent_RegistryData_Hkcu); case RegistryHiveXml.Hkmu: return(GuidIdXml.MsiComponent_RegistryData_Hkmu); default: throw new ArgumentOutOfRangeException("hive", hive, "Unknown Registry Hive value."); } }
/// <summary> /// Converts a RegistryData hive into a WiX Root. /// </summary> protected static RegistryRootType GetRoot(RegistryHiveXml hivexml) { switch (hivexml) { case RegistryHiveXml.Hkcr: return(RegistryRootType.HKCR); case RegistryHiveXml.Hklm: return(RegistryRootType.HKLM); case RegistryHiveXml.Hkcu: return(RegistryRootType.HKCU); case RegistryHiveXml.Hkmu: return(RegistryRootType.HKMU); default: throw new InvalidOperationException(string.Format("Unexpected RegistryData hive “{0}”.", hivexml)); } }