OSDN Git Service

initial build for opensource
[applistation/AppliStation.git] / na-get-lib / NaGet.Packages.Install / RegistriedUninstallers.cs
1 using System;\r
2 using System.Collections.Generic;\r
3 using System.Text.RegularExpressions;\r
4 using Microsoft.Win32;\r
5 \r
6 namespace NaGet.Packages.Install\r
7 {\r
8         /// <summary>\r
9         /// レジストリ登録されているアンインストーラに関するユーティリティ\r
10         /// </summary>\r
11         public sealed class RegistriedUninstallers\r
12         {\r
13                 /// <summary>\r
14                 /// アンインストーラのレジストリの格納されているルートキーの文字列表現\r
15                 /// </summary>\r
16                 public const string UninstallersKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";\r
17                 \r
18                 /// <summary>\r
19                 /// アンインストーラのレジストリの格納されているルートキーの文字列表現\r
20                 /// </summary>\r
21                 public const string UninstallersKeyWow6432 = @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall";\r
22                 \r
23                 /// <summary>\r
24                 /// アンインストーラのレジストリのキーを返す。\r
25                 /// </summary>\r
26                 public static IEnumerable<RegistryKey> RegistryKeies {\r
27                         get {\r
28                                 RegistryKey key = null;\r
29                                 \r
30                                 // UninstallersKey\r
31                                 try {\r
32                                         key = Registry.LocalMachine.OpenSubKey(UninstallersKey, false);\r
33                                 } catch (System.Security.SecurityException) {\r
34                                 } \r
35                                 if (key != null) {\r
36                                         yield return key;\r
37                                         key.Close();\r
38                                 }\r
39                                 \r
40                                 // UninstallersKeyWow6432\r
41                                 try {\r
42                                         key = Registry.LocalMachine.OpenSubKey(UninstallersKeyWow6432, false);\r
43                                 } catch (System.Security.SecurityException) {\r
44                                 } \r
45                                 if (key != null) {\r
46                                         yield return key;\r
47                                         key.Close();\r
48                                 }\r
49                         }\r
50                 }\r
51         \r
52                 \r
53                 /// <summary>\r
54                 /// アンインストーラをイテレートする\r
55                 /// </summary>\r
56                 public static IEnumerable<UninstallInformation> Uninstallers {\r
57                         get {\r
58                                 foreach (RegistryKey regkey in RegistryKeies) {\r
59                                         foreach (string key in regkey.GetSubKeyNames()) {\r
60                                                 UninstallInformation info;\r
61                                                 using (RegistryKey subregkey = regkey.OpenSubKey(key, false)) {\r
62                                                         info = UninstallInformation.NewInstance(subregkey);\r
63                                                 }\r
64                                                 \r
65                                                 if (info.IsOSPatch || info.IsSystemComponent || string.IsNullOrEmpty(info.DisplayName) ) {\r
66                                                         continue;\r
67                                                 }\r
68                                                 \r
69                                                 yield return info;\r
70                                         }\r
71                                 }\r
72                         }\r
73                 }\r
74                 \r
75                 /// <summary>\r
76                 /// レジストリを走査してインストール済みのソフトを検出する\r
77                 /// </summary>\r
78                 /// <param name="list">\r
79                 /// 参照するパッケージリスト\r
80                 /// </param>\r
81                 /// <returns>\r
82                 /// インストール済みのパッケージを返すイテレータ\r
83                 /// </returns>\r
84                 public static IEnumerable<InstalledPackage> DetectInstalledPackages(PackageList<Package> list)\r
85                 {\r
86                         foreach (UninstallInformation info in RegistriedUninstallers.Uninstallers) {\r
87                                 foreach (Package pkg in list.Packages) {\r
88                                         if (pkg.Type != InstallerType.ARCHIVE && pkg.UninstallerKey != null) {\r
89                                                 Match match = Regex.Match(info.DisplayName, pkg.UninstallerKey);\r
90                                                 \r
91                                                 if (match.Success) {\r
92                                                         yield return InstalledPackage.PackageConverter(pkg, info);\r
93                                                         \r
94                                                         break;\r
95                                                 }\r
96                                         }// else continue;\r
97                                 }\r
98                         }\r
99                 }\r
100                 \r
101                 /// <summary>\r
102                 /// パッケージに対応するインストールパッケージを返す。\r
103                 /// インストール終了確認などに使用。\r
104                 /// </summary>\r
105                 /// <param name="pkg">対応するパッケージ</param>\r
106                 /// <returns>インストール情報</returns>\r
107                 public static InstalledPackage GetInstalledPackageFor(Package pkg)\r
108                 {\r
109                         if (pkg.Type == InstallerType.ARCHIVE || pkg.Type == InstallerType.CANNOT_INSTALL) {\r
110                                 return null;\r
111                         }\r
112                         \r
113                         foreach (UninstallInformation info in RegistriedUninstallers.Uninstallers) {\r
114                                 Match match = Regex.Match(info.DisplayName, pkg.UninstallerKey);\r
115                                 \r
116                                 if (match.Success) {\r
117                                         return InstalledPackage.PackageConverter(pkg, info);\r
118                                 }\r
119                         }\r
120                         return null;\r
121                 }\r
122         }\r
123 }\r