using System; using System.Collections.Generic; using System.Text.RegularExpressions; using Microsoft.Win32; namespace NaGet.Packages.Install { /// /// レジストリ登録されているアンインストーラに関するユーティリティ /// public sealed class RegistriedUninstallers { /// /// アンインストーラーのレジストリの格納されているルートキーの文字列表現 /// public const string UninstallersKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"; /// /// アンインストーラーのレジストリの格納されているルートキーの文字列表現 /// public const string UninstallersKeyWow6432 = @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"; /// /// アンインストーラーのレジストリのキーを返す。 /// public static IEnumerable RegistryKeies { get { RegistryKey key = null; // HKLM // UninstallersKey try { key = Registry.LocalMachine.OpenSubKey(UninstallersKey, false); } catch (System.Security.SecurityException) { } if (key != null) { yield return key; key.Close(); } // UninstallersKeyWow6432 try { key = Registry.LocalMachine.OpenSubKey(UninstallersKeyWow6432, false); } catch (System.Security.SecurityException) { } if (key != null) { yield return key; key.Close(); } // HKCU // UninstallersKey try { key = Registry.CurrentUser.OpenSubKey(UninstallersKey, false); } catch (System.Security.SecurityException) { } if (key != null) { yield return key; key.Close(); } // UninstallersKeyWow6432 try { key = Registry.CurrentUser.OpenSubKey(UninstallersKeyWow6432, false); } catch (System.Security.SecurityException) { } if (key != null) { yield return key; key.Close(); } } } /// /// アンインストーラーをイテレートする /// public static IEnumerable Uninstallers { get { foreach (RegistryKey regkey in RegistryKeies) { foreach (string key in regkey.GetSubKeyNames()) { UninstallInformation info; using (RegistryKey subregkey = regkey.OpenSubKey(key, false)) { info = UninstallInformation.NewInstance(subregkey); } if (info.IsOSPatch || info.IsSystemComponent || string.IsNullOrEmpty(info.DisplayName) ) { continue; } yield return info; } } } } /// /// レジストリを走査してインストール済みのソフトを検出する /// /// /// 参照するパッケージリスト /// /// /// インストール済みのパッケージを返すイテレータ /// public static IEnumerable DetectInstalledPackages(PackageList pkgList) { foreach (UninstallInformation info in RegistriedUninstallers.Uninstallers) { foreach (Package pkg in pkgList) { if (pkg.Type != InstallerType.ARCHIVE && pkg.Type != InstallerType.ITSELF && pkg.UninstallerKey != null) { Match match = Regex.Match(info.DisplayName, pkg.UninstallerKey); if (match.Success) { yield return InstalledPackage.PackageConverter(pkg, info); break; } }// else continue; } } } /// /// パッケージに対応するインストールパッケージを返す。 /// インストール終了確認などに使用。 /// /// 対応するパッケージ /// インストール情報 public static InstalledPackage GetInstalledPackageFor(Package pkg) { if (pkg.Type == InstallerType.ARCHIVE || pkg.Type == InstallerType.ITSELF || pkg.Type == InstallerType.CANNOT_INSTALL) { return null; } foreach (UninstallInformation info in RegistriedUninstallers.Uninstallers) { Match match = Regex.Match(info.DisplayName, pkg.UninstallerKey); if (match.Success) { return InstalledPackage.PackageConverter(pkg, info); } } return null; } } }