public static RegistryKey openKey(this UninstallBaseKey baseKey) { return(baseKey switch { UninstallBaseKey.LOCAL_MACHINE_UNINSTALL => Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall") !, UninstallBaseKey.CURRENT_USER_UNINSTALL => Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall") !, UninstallBaseKey.CLASSES_ROOT_INSTALLER_PRODUCTS => Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(@"Installer\Products") !, UninstallBaseKey.LOCAL_MACHINE_WOW6432NODE_UNINSTALL => Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall") !, UninstallBaseKey.CURRENT_USER_INSTALLER_PRODUCTS => Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Installer\Products") !, _ => throw new ArgumentOutOfRangeException(nameof(baseKey), baseKey, null) });
public ProgramToClean(UninstallBaseKey baseKey, ProgramSelector selector, string?setDisplayNameTo = null, ProgramModifications.DisplayIconGenerator?setDisplayIconUsing = null, bool?hide = null) { if (selector.displayName == null && selector.keyName == null) { throw new ArgumentException("The selector must not have a null keyName pattern and a displayName pattern. At least one of these properties must be non-null."); } this.selector = selector; this.selector.baseKey = baseKey; modifications = new ProgramModifications(setDisplayNameTo, setDisplayIconUsing, hide); }
public static void findAndAssignIcons(UninstallBaseKey baseKey) { using RegistryKey parentKey = baseKey.openKey(); foreach (string childKeyName in parentKey.GetSubKeyNames()) { using RegistryKey childKey = parentKey.OpenSubKey(childKeyName, true) !; if (childKey.GetValue(RegistryConstants.UNINSTALL_STRING) is string uninstallString) { IEnumerable <string> uninstallParts = CommandLineParser.splitArgs(uninstallString).ToArray(); try { string uninstallerPath = uninstallParts.First(); string?uninstallerDirectory = Path.GetDirectoryName(uninstallerPath); if (!string.IsNullOrEmpty(uninstallerDirectory) && childKey.GetValue(RegistryConstants.DISPLAY_NAME) is string displayName && !Path.GetFileName(uninstallerPath).Equals("msiexec.exe", StringComparison.InvariantCultureIgnoreCase) && childKey.GetValue(RegistryConstants.RELEASE_TYPE) as string != "Update" && childKey.GetValue(RegistryConstants.DISPLAY_ICON) == null) { // Console.WriteLine($"Scanning for {childKeyName} {displayName} EXEs with icons in {uninstallerDirectory}"); // Console.WriteLine($"Uninstall string = {uninstallString}"); IEnumerable <string> exeFiles = Directory.EnumerateFiles(uninstallerDirectory, "*.exe", SearchOption.AllDirectories); if (exeFiles.Where(exeFilename => Path.GetFullPath(exeFilename) != Path.GetFullPath(uninstallerPath) && !BLACKLISTED_ICON_EXES.Contains(Path.GetFileName(exeFilename).ToLowerInvariant()) && hasIcons(exeFilename)) .MaxBy(exeFilename => new FileInfo(exeFilename).Length) .FirstOrDefault() is { } largestNonUninstallerExeWithIcons) { Console.WriteLine($"Setting DisplayIcon of {childKeyName} to {largestNonUninstallerExeWithIcons}"); childKey.SetValue(RegistryConstants.DISPLAY_ICON, largestNonUninstallerExeWithIcons); } } } catch (ArgumentException e) { Console.WriteLine($"Failed to find icon for {childKeyName}: {e.Message}"); } } } }