OSDN Git Service

na-get-lib,タスクセットの改修(NaGetTaskSet2)および、そのUnitTestを追加
[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                                 // HKLM\r
31                                 \r
32                                 // UninstallersKey\r
33                                 try {\r
34                                         key = Registry.LocalMachine.OpenSubKey(UninstallersKey, false);\r
35                                 } catch (System.Security.SecurityException) {\r
36                                 } \r
37                                 if (key != null) {\r
38                                         yield return key;\r
39                                         key.Close();\r
40                                 }\r
41                                 \r
42                                 // UninstallersKeyWow6432\r
43                                 try {\r
44                                         key = Registry.LocalMachine.OpenSubKey(UninstallersKeyWow6432, false);\r
45                                 } catch (System.Security.SecurityException) {\r
46                                 } \r
47                                 if (key != null) {\r
48                                         yield return key;\r
49                                         key.Close();\r
50                                 }\r
51                                 \r
52                                 // HKCU\r
53                                 \r
54                                 // UninstallersKey\r
55                                 try {\r
56                                         key = Registry.CurrentUser.OpenSubKey(UninstallersKey, false);\r
57                                 } catch (System.Security.SecurityException) {\r
58                                 } \r
59                                 if (key != null) {\r
60                                         yield return key;\r
61                                         key.Close();\r
62                                 }\r
63                                 \r
64                                 // UninstallersKeyWow6432\r
65                                 try {\r
66                                         key = Registry.CurrentUser.OpenSubKey(UninstallersKeyWow6432, false);\r
67                                 } catch (System.Security.SecurityException) {\r
68                                 } \r
69                                 if (key != null) {\r
70                                         yield return key;\r
71                                         key.Close();\r
72                                 }\r
73                         }\r
74                 }\r
75         \r
76                 \r
77                 /// <summary>\r
78                 /// アンインストーラーをイテレートする\r
79                 /// </summary>\r
80                 public static IEnumerable<UninstallInformation> Uninstallers {\r
81                         get {\r
82                                 foreach (RegistryKey regkey in RegistryKeies) {\r
83                                         foreach (string key in regkey.GetSubKeyNames()) {\r
84                                                 UninstallInformation info;\r
85                                                 using (RegistryKey subregkey = regkey.OpenSubKey(key, false)) {\r
86                                                         info = UninstallInformation.NewInstance(subregkey);\r
87                                                 }\r
88                                                 \r
89                                                 if (info.IsOSPatch || info.IsSystemComponent || string.IsNullOrEmpty(info.DisplayName) ) {\r
90                                                         continue;\r
91                                                 }\r
92                                                 \r
93                                                 yield return info;\r
94                                         }\r
95                                 }\r
96                         }\r
97                 }\r
98                 \r
99                 /// <summary>\r
100                 /// レジストリを走査してインストール済みのソフトを検出する\r
101                 /// </summary>\r
102                 /// <param name="list">\r
103                 /// 参照するパッケージリスト\r
104                 /// </param>\r
105                 /// <returns>\r
106                 /// インストール済みのパッケージを返すイテレータ\r
107                 /// </returns>\r
108                 public static IEnumerable<InstalledPackage> DetectInstalledPackages(PackageList<Package> pkgList)\r
109                 {\r
110                         foreach (UninstallInformation info in RegistriedUninstallers.Uninstallers) {\r
111                                 foreach (Package pkg in pkgList) {\r
112                                         if (pkg.Type != InstallerType.ARCHIVE\r
113                                             && pkg.Type != InstallerType.ITSELF\r
114                                             && pkg.UninstallerKey != null) {\r
115                                                 Match match = Regex.Match(info.DisplayName, pkg.UninstallerKey);\r
116                                                 \r
117                                                 if (match.Success) {\r
118                                                         yield return InstalledPackage.PackageConverter(pkg, info);\r
119                                                         \r
120                                                         break;\r
121                                                 }\r
122                                         }// else continue;\r
123                                 }\r
124                         }\r
125                 }\r
126                 \r
127                 /// <summary>\r
128                 /// パッケージに対応するインストールパッケージを返す。\r
129                 /// インストール終了確認などに使用。\r
130                 /// </summary>\r
131                 /// <param name="pkg">対応するパッケージ</param>\r
132                 /// <returns>インストール情報</returns>\r
133                 public static InstalledPackage GetInstalledPackageFor(Package pkg)\r
134                 {\r
135                         if (pkg.Type == InstallerType.ARCHIVE\r
136                             || pkg.Type == InstallerType.ITSELF\r
137                             || pkg.Type == InstallerType.CANNOT_INSTALL) {\r
138                                 return null;\r
139                         }\r
140                         \r
141                         foreach (UninstallInformation info in RegistriedUninstallers.Uninstallers) {\r
142                                 Match match = Regex.Match(info.DisplayName, pkg.UninstallerKey);\r
143                                 \r
144                                 if (match.Success) {\r
145                                         return InstalledPackage.PackageConverter(pkg, info);\r
146                                 }\r
147                         }\r
148                         return null;\r
149                 }\r
150         }\r
151 }\r