/// <summary> /// Installs all registry values packaged in the installer to the local device /// </summary> public void InstallRegValues() { m_failedRegVals = 0; if (m_descriptor.RegistryKeys == null) { return; } foreach (InstallerRegistryKey key in m_descriptor.RegistryKeys) { string keyName = string.Format("{0}{1}", m_descriptor.GetHiveByHiveID(key.HiveID).ToString(), m_descriptor.GetSubkeyByHiveID(key.HiveID)); string valueName = string.Copy(key.ValueName); Microsoft.Win32.RegistryValueKind valueKind = Microsoft.Win32.RegistryValueKind.Unknown; switch (key.KeyType & (~RegistyKeyType.NoClobber)) { case RegistyKeyType.Binary: valueKind = Microsoft.Win32.RegistryValueKind.Binary; break; case RegistyKeyType.MultiString: valueKind = Microsoft.Win32.RegistryValueKind.MultiString; break; case RegistyKeyType.Number: valueKind = Microsoft.Win32.RegistryValueKind.DWord; break; case RegistyKeyType.String: valueKind = Microsoft.Win32.RegistryValueKind.String; break; } byte[] data = (byte[])key.Data.Clone(); RegistryInstallInfo registryInfo = new RegistryInstallInfo(((key.KeyType & RegistyKeyType.NoClobber) == RegistyKeyType.NoClobber), keyName, valueName, valueKind, data); try { OnInstallRegValue(ref registryInfo); } catch (Exception ex) { if (RegistryFailure != null) { bool cancel = false; RegistryFailure(registryInfo, ex, ref cancel); if (!cancel) { m_failedRegVals++; } } else { m_failedRegVals++; } } } }
/// <summary> /// Called when a registry value is extracted from the installer and needs to be installed to the device OS /// </summary> /// <param name="registryInfo"></param> public virtual void OnInstallRegValue(ref RegistryInstallInfo registryInfo) { if (registryInfo.NoClobber) { // see if the key is already there if (Microsoft.Win32.Registry.GetValue(registryInfo.KeyName, registryInfo.ValueName, null) != null) { return; } } switch (registryInfo.ValueKind) { case RegistryValueKind.Binary: Registry.SetValue(registryInfo.KeyName, registryInfo.ValueName, registryInfo.Data, RegistryValueKind.Binary); break; case RegistryValueKind.String: Registry.SetValue(registryInfo.KeyName, registryInfo.ValueName, ReplaceSpecialFolderStrings(Encoding.ASCII.GetString(registryInfo.Data, 0, registryInfo.Data.Length - 1)), RegistryValueKind.String); break; case RegistryValueKind.DWord: Registry.SetValue(registryInfo.KeyName, registryInfo.ValueName, BitConverter.ToInt32(registryInfo.Data, 0), RegistryValueKind.DWord); break; case RegistryValueKind.MultiString: List <string> stringList = new List <string>(); int start = 0; int end = 0; while (registryInfo.Data[end] != 0) { for (int i = start; i < registryInfo.Data.Length; i++) { if (registryInfo.Data[start + i] == 0) { end += i; break; } } string item = ReplaceSpecialFolderStrings(Encoding.ASCII.GetString(registryInfo.Data, start, end - start)); stringList.Add(item); start += end; } Registry.SetValue(registryInfo.KeyName, registryInfo.ValueName, stringList, RegistryValueKind.MultiString); break; } }