markus / MarkusAutoUpdate / src / NetSparkle / Configurations / RegistryConfiguration.cs @ 56f4174c
이력 | 보기 | 이력해설 | 다운로드 (9.58 KB)
1 | d8f5045e | taeseongkim | using System; |
---|---|---|---|
2 | using System.Globalization; |
||
3 | using Microsoft.Win32; |
||
4 | using NetSparkleUpdater.AssemblyAccessors; |
||
5 | using NetSparkleUpdater.Interfaces; |
||
6 | |||
7 | namespace NetSparkleUpdater.Configurations |
||
8 | { |
||
9 | /// <summary> |
||
10 | /// This class handles all registry values which are used from sparkle to handle |
||
11 | /// update intervalls. All values are stored in HKCU\Software\Vendor\AppName which |
||
12 | /// will be read ot from the assembly information. All values are of the REG_SZ |
||
13 | /// type, no matter what their "logical" type is. The following options are |
||
14 | /// available: |
||
15 | /// |
||
16 | /// CheckForUpdate - Boolean - Whether NetSparkle should check for updates |
||
17 | /// LastCheckTime - time_t - Time of last check |
||
18 | /// SkipThisVersion - String - If the user skipped an update, then the version to ignore is stored here (e.g. "1.4.3") |
||
19 | /// DidRunOnce - Boolean - Check only one time when the app launched |
||
20 | /// </summary> |
||
21 | public class RegistryConfiguration : Configuration |
||
22 | { |
||
23 | private const string DateTimeFormat = "yyyy-MM-dd HH:mm:ss"; |
||
24 | private string _registryPath; |
||
25 | |||
26 | /// <summary> |
||
27 | /// Constructor for a configuration that saves and loads information from the Windows registry. |
||
28 | /// This should only be used on Windows! |
||
29 | /// </summary> |
||
30 | /// <param name="assemblyAccessor">Object that accesses version, title, etc. info for the application |
||
31 | /// you would like to check for updates for</param> |
||
32 | public RegistryConfiguration(IAssemblyAccessor assemblyAccessor) |
||
33 | : this(assemblyAccessor, "") |
||
34 | { } |
||
35 | |||
36 | /// <summary> |
||
37 | /// Constructor for a configuration that saves and loads information from the Windows registry. |
||
38 | /// This should only be used on Windows! |
||
39 | /// </summary> |
||
40 | /// <param name="assemblyAccessor">Object that accesses version, title, etc. info for the application |
||
41 | /// you would like to check for updates for</param> |
||
42 | /// <param name="registryPath">Location in the registry where configuration data should be stored and |
||
43 | /// loaded from</param> |
||
44 | public RegistryConfiguration(IAssemblyAccessor assemblyAccessor, string registryPath) |
||
45 | : base(assemblyAccessor) |
||
46 | { |
||
47 | _registryPath = registryPath; |
||
48 | try |
||
49 | { |
||
50 | // build the reg path |
||
51 | string regPath = BuildRegistryPath(); |
||
52 | |||
53 | // load the values |
||
54 | LoadValuesFromPath(regPath); |
||
55 | } |
||
56 | catch (NetSparkleException e) |
||
57 | { |
||
58 | // disable update checks when exception occurred -- can't read/save necessary update file |
||
59 | CheckForUpdate = false; |
||
60 | throw new NetSparkleException("Can't read/save configuration data: " + e.Message); |
||
61 | } |
||
62 | } |
||
63 | |||
64 | /// <summary> |
||
65 | /// Touches to profile time |
||
66 | /// </summary> |
||
67 | public override void TouchProfileTime() |
||
68 | { |
||
69 | base.TouchProfileTime(); |
||
70 | // save the values |
||
71 | SaveValuesToPath(BuildRegistryPath()); |
||
72 | } |
||
73 | |||
74 | /// <summary> |
||
75 | /// Touches the check time to now, should be used after a check directly |
||
76 | /// </summary> |
||
77 | public override void TouchCheckTime() |
||
78 | { |
||
79 | base.TouchCheckTime(); |
||
80 | // save the values |
||
81 | SaveValuesToPath(BuildRegistryPath()); |
||
82 | } |
||
83 | |||
84 | /// <summary> |
||
85 | /// This method allows to skip a specific version |
||
86 | /// </summary> |
||
87 | /// <param name="version">the version to skeip</param> |
||
88 | public override void SetVersionToSkip(string version) |
||
89 | { |
||
90 | base.SetVersionToSkip(version); |
||
91 | SaveValuesToPath(BuildRegistryPath()); |
||
92 | } |
||
93 | |||
94 | /// <summary> |
||
95 | /// Reloads the configuration object |
||
96 | /// </summary> |
||
97 | public override void Reload() |
||
98 | { |
||
99 | LoadValuesFromPath(BuildRegistryPath()); |
||
100 | } |
||
101 | |||
102 | /// <summary> |
||
103 | /// This function build a valid registry path in dependecy to the |
||
104 | /// assembly information |
||
105 | /// </summary> |
||
106 | public virtual string BuildRegistryPath() |
||
107 | { |
||
108 | if (!string.IsNullOrEmpty(_registryPath)) |
||
109 | { |
||
110 | return _registryPath; |
||
111 | } |
||
112 | else |
||
113 | { |
||
114 | if (string.IsNullOrEmpty(AssemblyAccessor.AssemblyCompany) || string.IsNullOrEmpty(AssemblyAccessor.AssemblyProduct)) |
||
115 | { |
||
116 | throw new NetSparkleException("Error: NetSparkleUpdater is missing the company or productname tag in the assembly accessor (" |
||
117 | + AssemblyAccessor.GetType() + ")"); |
||
118 | } |
||
119 | |||
120 | _registryPath = "Software\\"; |
||
121 | if (!string.IsNullOrWhiteSpace(AssemblyAccessor.AssemblyCompany)) |
||
122 | { |
||
123 | _registryPath += AssemblyAccessor.AssemblyCompany + "\\"; |
||
124 | } |
||
125 | if (!string.IsNullOrWhiteSpace(AssemblyAccessor.AssemblyProduct)) |
||
126 | { |
||
127 | _registryPath += AssemblyAccessor.AssemblyProduct + "\\"; |
||
128 | } |
||
129 | _registryPath += "AutoUpdate"; |
||
130 | return _registryPath; |
||
131 | } |
||
132 | } |
||
133 | |||
134 | private string ConvertDateToString(DateTime dt) |
||
135 | { |
||
136 | return dt.ToString(DateTimeFormat, CultureInfo.InvariantCulture); |
||
137 | } |
||
138 | |||
139 | private DateTime ConvertStringToDate(string str) |
||
140 | { |
||
141 | return DateTime.ParseExact(str, DateTimeFormat, CultureInfo.InvariantCulture); |
||
142 | } |
||
143 | |||
144 | /// <summary> |
||
145 | /// This method loads the values from registry |
||
146 | /// </summary> |
||
147 | /// <param name="regPath">the registry path</param> |
||
148 | /// <returns><c>true</c> if the items were loaded</returns> |
||
149 | private bool LoadValuesFromPath(string regPath) |
||
150 | { |
||
151 | RegistryKey key = Registry.CurrentUser.OpenSubKey(regPath); |
||
152 | if (key == null) |
||
153 | { |
||
154 | SaveDidRunOnceAsTrue(regPath); |
||
155 | return false; |
||
156 | } |
||
157 | else |
||
158 | { |
||
159 | // read out |
||
160 | string strCheckForUpdate = key.GetValue("CheckForUpdate", "True") as string; |
||
161 | string strLastCheckTime = key.GetValue("LastCheckTime", ConvertDateToString(new DateTime(0))) as string; |
||
162 | string strSkipThisVersion = key.GetValue("SkipThisVersion", "") as string; |
||
163 | string strDidRunOnc = key.GetValue("DidRunOnce", "False") as string; |
||
164 | string strProfileTime = key.GetValue("LastProfileUpdate", ConvertDateToString(new DateTime(0))) as string; |
||
165 | string strPreviousVersion = key.GetValue("PreviousVersionRun", "") as string; |
||
166 | |||
167 | // convert the right datatypes |
||
168 | CheckForUpdate = Convert.ToBoolean(strCheckForUpdate); |
||
169 | try |
||
170 | { |
||
171 | LastCheckTime = ConvertStringToDate(strLastCheckTime); |
||
172 | } |
||
173 | catch (FormatException) |
||
174 | { |
||
175 | LastCheckTime = new DateTime(0); |
||
176 | } |
||
177 | |||
178 | LastVersionSkipped = strSkipThisVersion; |
||
179 | DidRunOnce = Convert.ToBoolean(strDidRunOnc); |
||
180 | IsFirstRun = !DidRunOnce; |
||
181 | PreviousVersionOfSoftwareRan = strPreviousVersion; |
||
182 | if (IsFirstRun) |
||
183 | { |
||
184 | SaveDidRunOnceAsTrue(regPath); |
||
185 | } |
||
186 | else |
||
187 | { |
||
188 | SaveValuesToPath(regPath); // so PreviousVersionRun is saved |
||
189 | } |
||
190 | try |
||
191 | { |
||
192 | LastConfigUpdate = ConvertStringToDate(strProfileTime); |
||
193 | } |
||
194 | catch (FormatException) |
||
195 | { |
||
196 | LastConfigUpdate = new DateTime(0); |
||
197 | } |
||
198 | } |
||
199 | return true; |
||
200 | } |
||
201 | |||
202 | private void SaveDidRunOnceAsTrue(string regPath) |
||
203 | { |
||
204 | var initialValue = DidRunOnce; |
||
205 | DidRunOnce = true; |
||
206 | SaveValuesToPath(regPath); // save it so next time we load DidRunOnce is true |
||
207 | DidRunOnce = initialValue; // so data is correct to user of Configuration class |
||
208 | } |
||
209 | |||
210 | /// <summary> |
||
211 | /// This method store the information into registry |
||
212 | /// </summary> |
||
213 | /// <param name="regPath">the registry path</param> |
||
214 | /// <returns><c>true</c> if the values were saved to the registry</returns> |
||
215 | private bool SaveValuesToPath(string regPath) |
||
216 | { |
||
217 | RegistryKey key = Registry.CurrentUser.CreateSubKey(regPath); |
||
218 | if (key == null) |
||
219 | { |
||
220 | return false; |
||
221 | } |
||
222 | |||
223 | // convert to regsz |
||
224 | string strCheckForUpdate = true.ToString(); // always check for updates next time! |
||
225 | string strLastCheckTime = ConvertDateToString(LastCheckTime); |
||
226 | string strSkipThisVersion = LastVersionSkipped; |
||
227 | string strDidRunOnc = DidRunOnce.ToString(); |
||
228 | string strProfileTime = ConvertDateToString(LastConfigUpdate); |
||
229 | string strPreviousVersion = InstalledVersion; |
||
230 | |||
231 | // set the values |
||
232 | key.SetValue("CheckForUpdate", strCheckForUpdate, RegistryValueKind.String); |
||
233 | key.SetValue("LastCheckTime", strLastCheckTime, RegistryValueKind.String); |
||
234 | key.SetValue("SkipThisVersion", strSkipThisVersion, RegistryValueKind.String); |
||
235 | key.SetValue("DidRunOnce", strDidRunOnc, RegistryValueKind.String); |
||
236 | key.SetValue("LastProfileUpdate", strProfileTime, RegistryValueKind.String); |
||
237 | key.SetValue("PreviousVersionRun", strPreviousVersion, RegistryValueKind.String); |
||
238 | |||
239 | return true; |
||
240 | } |
||
241 | } |
||
242 | } |