| 1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. |
| 2 | |
| 3 | namespace WixTestTools |
| 4 | { |
| 5 | using System; |
| 6 | using Microsoft.Win32; |
| 7 | |
| 8 | public class GenericArpRegistration |
| 9 | { |
| 10 | public const string UNINSTALL_KEY = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall"; |
| 11 | public const string UNINSTALL_KEY_WOW6432NODE = "SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall"; |
| 12 | |
| 13 | public const string REGISTRY_ARP_INSTALLED = "Installed"; |
| 14 | public const string REGISTRY_ARP_DISPLAY_ICON = "DisplayIcon"; |
| 15 | public const string REGISTRY_ARP_DISPLAY_NAME = "DisplayName"; |
| 16 | public const string REGISTRY_ARP_DISPLAY_VERSION = "DisplayVersion"; |
| 17 | public const string REGISTRY_ARP_ESTIMATED_SIZE = "EstimatedSize"; |
| 18 | public const string REGISTRY_ARP_INSTALL_DATE = "InstallDate"; |
| 19 | public const string REGISTRY_ARP_PUBLISHER = "Publisher"; |
| 20 | public const string REGISTRY_ARP_HELP_LINK = "HelpLink"; |
| 21 | public const string REGISTRY_ARP_HELP_TELEPHONE = "HelpTelephone"; |
| 22 | public const string REGISTRY_ARP_URL_INFO_ABOUT = "URLInfoAbout"; |
| 23 | public const string REGISTRY_ARP_URL_UPDATE_INFO = "URLUpdateInfo"; |
| 24 | public const string REGISTRY_ARP_COMMENTS = "Comments"; |
| 25 | public const string REGISTRY_ARP_CONTACT = "Contact"; |
| 26 | public const string REGISTRY_ARP_NO_MODIFY = "NoModify"; |
| 27 | public const string REGISTRY_ARP_MODIFY_PATH = "ModifyPath"; |
| 28 | public const string REGISTRY_ARP_NO_ELEVATE_ON_MODIFY = "NoElevateOnModify"; |
| 29 | public const string REGISTRY_ARP_NO_REMOVE = "NoRemove"; |
| 30 | public const string REGISTRY_ARP_SYSTEM_COMPONENT = "SystemComponent"; |
| 31 | public const string REGISTRY_ARP_QUIET_UNINSTALL_STRING = "QuietUninstallString"; |
| 32 | public const string REGISTRY_ARP_UNINSTALL_STRING = "UninstallString"; |
| 33 | public const string REGISTRY_ARP_VERSION_MAJOR = "VersionMajor"; |
| 34 | public const string REGISTRY_ARP_VERSION_MINOR = "VersionMinor"; |
| 35 | |
| 36 | public RegistryKey BaseKey { get; set; } |
| 37 | |
| 38 | public string KeyPath { get; set; } |
| 39 | |
| 40 | public string DisplayName { get; set; } |
| 41 | |
| 42 | public string DisplayVersion { get; set; } |
| 43 | |
| 44 | public int? EstimatedSize { get; set; } |
| 45 | |
| 46 | public string InstallDate { get; set; } |
| 47 | |
| 48 | public int? Installed { get; set; } |
| 49 | |
| 50 | public string ModifyPath { get; set; } |
| 51 | |
| 52 | public string Publisher { get; set; } |
| 53 | |
| 54 | public int? SystemComponent { get; set; } |
| 55 | |
| 56 | public string QuietUninstallString { get; set; } |
| 57 | |
| 58 | public string QuietUninstallCommand { get; set; } |
| 59 | |
| 60 | public string QuietUninstallCommandArguments { get; set; } |
| 61 | |
| 62 | public string UninstallCommand { get; set; } |
| 63 | |
| 64 | public string UninstallCommandArguments { get; set; } |
| 65 | |
| 66 | public string UninstallString { get; set; } |
| 67 | |
| 68 | public string UrlInfoAbout { get; set; } |
| 69 | |
| 70 | public string UrlUpdateInfo { get; set; } |
| 71 | |
| 72 | public static bool TryGetPerMachineRegistrationById(string id, bool x64, out GenericArpRegistration registration) |
| 73 | { |
| 74 | return TryGetRegistrationById(id, x64, false, out registration); |
| 75 | } |
| 76 | |
| 77 | public static bool TryGetPerUserRegistrationById(string id, out GenericArpRegistration registration) |
| 78 | { |
| 79 | return TryGetRegistrationById(id, true, true, out registration); |
| 80 | } |
| 81 | |
| 82 | private static bool TryGetRegistrationById(string id, bool x64, bool perUser, out GenericArpRegistration registration) |
| 83 | { |
| 84 | registration = GetGenericArpRegistration(id, x64, perUser, key => new GenericArpRegistration()); |
| 85 | return registration != null; |
| 86 | } |
| 87 | |
| 88 | protected static T GetGenericArpRegistration<T>(string id, bool x64, bool perUser, Func<RegistryKey, T> fnCreate) |
| 89 | where T : GenericArpRegistration |
| 90 | { |
| 91 | var baseKey = perUser ? Registry.CurrentUser : Registry.LocalMachine; |
| 92 | var baseKeyPath = x64 ? UNINSTALL_KEY : UNINSTALL_KEY_WOW6432NODE; |
| 93 | var registrationKeyPath = $"{baseKeyPath}\\{id}"; |
| 94 | using var idKey = baseKey.OpenSubKey(registrationKeyPath); |
| 95 | |
| 96 | if (idKey == null) |
| 97 | { |
| 98 | return null; |
| 99 | } |
| 100 | |
| 101 | var registration = fnCreate(idKey); |
| 102 | |
| 103 | registration.BaseKey = baseKey; |
| 104 | registration.KeyPath = registrationKeyPath; |
| 105 | |
| 106 | registration.DisplayName = idKey.GetValue(REGISTRY_ARP_DISPLAY_NAME) as string; |
| 107 | registration.DisplayVersion = idKey.GetValue(REGISTRY_ARP_DISPLAY_VERSION) as string; |
| 108 | registration.EstimatedSize = idKey.GetValue(REGISTRY_ARP_ESTIMATED_SIZE) as int?; |
| 109 | registration.InstallDate = idKey.GetValue(REGISTRY_ARP_INSTALL_DATE) as string; |
| 110 | registration.Installed = idKey.GetValue(REGISTRY_ARP_INSTALLED) as int?; |
| 111 | registration.ModifyPath = idKey.GetValue(REGISTRY_ARP_MODIFY_PATH) as string; |
| 112 | registration.Publisher = idKey.GetValue(REGISTRY_ARP_PUBLISHER) as string; |
| 113 | registration.SystemComponent = idKey.GetValue(REGISTRY_ARP_SYSTEM_COMPONENT) as int?; |
| 114 | registration.UrlInfoAbout = idKey.GetValue(REGISTRY_ARP_URL_INFO_ABOUT) as string; |
| 115 | registration.UrlUpdateInfo = idKey.GetValue(REGISTRY_ARP_URL_UPDATE_INFO) as string; |
| 116 | |
| 117 | registration.QuietUninstallString = idKey.GetValue(REGISTRY_ARP_QUIET_UNINSTALL_STRING) as string; |
| 118 | if (!String.IsNullOrEmpty(registration.QuietUninstallString)) |
| 119 | { |
| 120 | var closeQuote = registration.QuietUninstallString.IndexOf("\"", 1); |
| 121 | if (closeQuote > 0) |
| 122 | { |
| 123 | registration.QuietUninstallCommand = registration.QuietUninstallString.Substring(1, closeQuote - 1).Trim(); |
| 124 | registration.QuietUninstallCommandArguments = registration.QuietUninstallString.Substring(closeQuote + 1).Trim(); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | registration.UninstallString = idKey.GetValue(REGISTRY_ARP_UNINSTALL_STRING) as string; |
| 129 | if (!String.IsNullOrEmpty(registration.UninstallString)) |
| 130 | { |
| 131 | var closeQuote = registration.UninstallString.IndexOf("\"", 1); |
| 132 | if (closeQuote > 0) |
| 133 | { |
| 134 | registration.UninstallCommand = registration.UninstallString.Substring(1, closeQuote - 1).Trim(); |
| 135 | registration.UninstallCommandArguments = registration.UninstallString.Substring(closeQuote + 1).Trim(); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | return registration; |
| 140 | } |
| 141 | |
| 142 | public void Delete() |
| 143 | { |
| 144 | this.BaseKey.DeleteSubKeyTree(this.KeyPath); |
| 145 | } |
| 146 | } |
| 147 | } |