markus / MarkusAutoUpdate / src / NetSparkle.Samples.NetFramework.WPF / MainWindow.xaml.cs @ 8f86cf42
이력 | 보기 | 이력해설 | 다운로드 (11.8 KB)
1 |
using NetSparkle.TestAppWPF.Common; |
---|---|
2 |
using NetSparkleUpdater.Events; |
3 |
using NetSparkleUpdater.SignatureVerifiers; |
4 |
using System; |
5 |
using System.Diagnostics; |
6 |
using System.Drawing; |
7 |
using System.IO; |
8 |
using System.IO.Compression; |
9 |
using System.Linq; |
10 |
using System.Security; |
11 |
using System.Threading.Tasks; |
12 |
using System.Windows; |
13 |
|
14 |
|
15 |
namespace NetSparkleUpdater.Samples.NetFramework.WPF |
16 |
{ |
17 |
/// <summary> |
18 |
/// Interaction logic for MainWindow.xaml |
19 |
/// </summary> |
20 |
public partial class MainWindow : Window |
21 |
{ |
22 |
private SparkleUpdater _sparkle; |
23 |
private const string InstallPath = @"C:\Program Files\Doftech\MARKUS"; |
24 |
private string KcomPath = @"C:\Program Files\Doftech\MARKUS\KCOM.exe"; |
25 |
|
26 |
private string downloadFile; |
27 |
private string tempStoragePath; |
28 |
|
29 |
private NetSparkle.TestAppWPF.ViewModels.MainViewModel viewModel; |
30 |
|
31 |
public MainWindow() |
32 |
{ |
33 |
InitializeComponent(); |
34 |
viewModel = new NetSparkle.TestAppWPF.ViewModels.MainViewModel(); |
35 |
this.DataContext = viewModel; |
36 |
|
37 |
//remove the netsparkle key from registry z |
38 |
//try |
39 |
//{ |
40 |
// Microsoft.Win32.Registry.CurrentUser.DeleteSubKeyTree("Software\\Microsoft\\NetSparkle.TestAppNetCoreWPF"); |
41 |
//} |
42 |
//catch (Exception ex) |
43 |
//{ |
44 |
// System.Diagnostics.Debug.WriteLine(ex); |
45 |
//} |
46 |
|
47 |
// set icon in project properties! |
48 |
try |
49 |
{ |
50 |
string manifestModuleName = System.Reflection.Assembly.GetEntryAssembly().ManifestModule.FullyQualifiedName; |
51 |
var icon = System.Drawing.Icon.ExtractAssociatedIcon(manifestModuleName); |
52 |
|
53 |
tempStoragePath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "MARKUS", System.IO.Path.GetRandomFileName()); |
54 |
|
55 |
App.logger.Info($"Temp Strorage Create : {tempStoragePath}"); |
56 |
|
57 |
if (!Directory.Exists(tempStoragePath)) |
58 |
{ |
59 |
Directory.CreateDirectory(tempStoragePath); |
60 |
} |
61 |
|
62 |
if (!Directory.Exists(InstallPath)) |
63 |
{ |
64 |
Directory.CreateDirectory(InstallPath); |
65 |
} |
66 |
|
67 |
//File.Copy(KcomPath, System.IO.Path.Combine(tempStoragePath, "Kcom.exe")); |
68 |
|
69 |
//KcomPath = System.IO.Path.Combine(tempStoragePath, "Kcom.exe"); |
70 |
|
71 |
if (!File.Exists(KcomPath)) |
72 |
{ |
73 |
File.Copy(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "kcom.exe"), KcomPath); |
74 |
} |
75 |
|
76 |
_sparkle = new SparkleUpdater(App.AppCastUri, new Ed25519Checker(Enums.SecurityMode.Unsafe, null, "NetSparkle_Ed25519.pub"), KcomPath) |
77 |
{ |
78 |
UIFactory = new NetSparkleUpdater.UI.WPF.UIFactory(NetSparkleUpdater.UI.WPF.IconUtilities.ToImageSource(icon)), |
79 |
ShowsUIOnMainThread = true, |
80 |
TmpDownloadFilePath = tempStoragePath, |
81 |
UserInteractionMode = Enums.UserInteractionMode.DownloadNoInstall, |
82 |
//UseNotificationToast = true |
83 |
RelaunchAfterUpdate = true, |
84 |
SecurityProtocolType = System.Net.SecurityProtocolType.Tls12 |
85 |
}; |
86 |
|
87 |
// TLS 1.2 required by GitHub (https://developer.github.com/changes/2018-02-01-weak-crypto-removal-notice/) |
88 |
_sparkle.DownloadFinished += FullUpdate_DownloadFileIsReady; |
89 |
_sparkle.UpdateCheckFinished += _sparkle_UpdateCheckFinished; |
90 |
_sparkle.DownloadHadError += _sparkle_DownloadError; |
91 |
_sparkle.DownloadMadeProgress += _sparkle_DownloadMadeProgress; |
92 |
//_sparkle.StartLoop(true, true); |
93 |
Updater(); |
94 |
App.logger.Info("main Initialize"); |
95 |
|
96 |
this.Loaded += MainWindow_Loaded; |
97 |
} |
98 |
catch (Exception ex) |
99 |
{ |
100 |
App.logger.Error("main Initialize", ex); |
101 |
} |
102 |
|
103 |
} |
104 |
|
105 |
private void _sparkle_DownloadMadeProgress(object sender, AppCastItem item, ItemDownloadProgressEventArgs args) |
106 |
{ |
107 |
viewModel.DownloadProgress = args.ProgressPercentage; |
108 |
} |
109 |
|
110 |
private void _sparkle_DownloadError(AppCastItem item, string path, Exception exception) |
111 |
{ |
112 |
App.logger.Error($"Download Error", exception); |
113 |
} |
114 |
|
115 |
private async void Updater() |
116 |
{ |
117 |
App.logger.Info("main Load"); |
118 |
try |
119 |
{ |
120 |
var _updateInfo = await _sparkle.CheckForUpdatesQuietly(); |
121 |
|
122 |
if (_updateInfo != null) |
123 |
{ |
124 |
App.logger.Info("Update Status : " + _updateInfo.Status); |
125 |
|
126 |
switch (_updateInfo.Status) |
127 |
{ |
128 |
case Enums.UpdateStatus.UpdateAvailable: |
129 |
this.Visibility = Visibility.Visible; |
130 |
//string a = "There's an update available!"; |
131 |
var castitem = _updateInfo.Updates.First(); |
132 |
|
133 |
viewModel.UpdateVersion = castitem.Version; |
134 |
System.Threading.Thread.Sleep(1000); |
135 |
|
136 |
if (App.IsExternal) |
137 |
{ |
138 |
if (!string.IsNullOrWhiteSpace(castitem.DownloadLink)) |
139 |
{ |
140 |
var uri = UriHelper.ChangeAddress(new Uri(castitem.DownloadLink), App.ExternalAddress); |
141 |
castitem.DownloadLink = uri.ToString(); |
142 |
} |
143 |
|
144 |
if (!string.IsNullOrWhiteSpace(castitem.ReleaseNotesLink)) |
145 |
{ |
146 |
var uri2 = UriHelper.ChangeAddress(new Uri(castitem.ReleaseNotesLink), App.ExternalAddress); |
147 |
castitem.ReleaseNotesLink = uri2.ToString(); |
148 |
} |
149 |
|
150 |
} |
151 |
|
152 |
await _sparkle.InitAndBeginDownload(castitem); |
153 |
|
154 |
App.logger.Info($"Update available Version : {castitem.Version} Link : {castitem.DownloadLink}"); |
155 |
|
156 |
break; |
157 |
case Enums.UpdateStatus.UpdateNotAvailable: |
158 |
case Enums.UpdateStatus.CouldNotDetermine: |
159 |
_sparkle.CloseApplication += () => |
160 |
{ |
161 |
App.logger.Info($"Start Markus : {App.KcomParam}"); |
162 |
|
163 |
Process.Start(KcomPath, App.KcomParam); |
164 |
Application.Current.Shutdown(); |
165 |
}; |
166 |
|
167 |
App.logger.Info($"Update Not available. Application Exit."); |
168 |
await _sparkle.QuitApplication(); |
169 |
|
170 |
break; |
171 |
} |
172 |
} |
173 |
else |
174 |
{ |
175 |
if (_updateInfo != null) |
176 |
{ |
177 |
App.logger.Error("Update Is Null"); |
178 |
Process.Start(KcomPath, App.KcomParam); |
179 |
} |
180 |
} |
181 |
} |
182 |
catch (Exception ex) |
183 |
{ |
184 |
App.logger.Error($"main Load", ex); |
185 |
} |
186 |
|
187 |
} |
188 |
|
189 |
private async void MainWindow_Loaded(object sender, RoutedEventArgs e) |
190 |
{ |
191 |
|
192 |
} |
193 |
|
194 |
private void _sparkle_UpdateCheckFinished(object sender, Enums.UpdateStatus status) |
195 |
{ |
196 |
|
197 |
} |
198 |
|
199 |
//[PermissionSet(SecurityAction.Demand, Name = "FullTrust")] |
200 |
private void FullUpdate_DownloadFileIsReady(AppCastItem item, string downloadPath) |
201 |
{ |
202 |
viewModel.IsWait = true; |
203 |
|
204 |
ExtractFiles(InstallPath,tempStoragePath,downloadPath,KcomPath, App.KcomParam); |
205 |
|
206 |
//ExtractProcess(item, downloadPath); |
207 |
} |
208 |
|
209 |
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")] |
210 |
private void ExtractFiles(string InstallPath, string tempStoragePath, string downloadFile, string KcomPath, string Param) |
211 |
{ |
212 |
viewModel.IsWait = true; |
213 |
try |
214 |
{ |
215 |
var extractFolder = Path.Combine(tempStoragePath, "Extract"); |
216 |
|
217 |
ZipFile.ExtractToDirectory(downloadFile, extractFolder); |
218 |
|
219 |
foreach (var file in Directory.EnumerateFiles(extractFolder, "*.*", SearchOption.AllDirectories)) |
220 |
{ |
221 |
var target = System.IO.Path.Combine(InstallPath, file.Replace(extractFolder, "").Substring(1)); |
222 |
|
223 |
var dir = Path.GetDirectoryName(target); |
224 |
|
225 |
if (!Directory.Exists(dir)) |
226 |
{ |
227 |
Directory.CreateDirectory(dir); |
228 |
} |
229 |
|
230 |
File.Copy(file, target, FileExits(target)); |
231 |
App.logger.Info($"Copy - {target}"); |
232 |
} |
233 |
|
234 |
Process.Start(KcomPath, Param); |
235 |
} |
236 |
catch (Exception ex) |
237 |
{ |
238 |
App.logger.Error($"ExtractFile Error", ex); |
239 |
|
240 |
if (ex.InnerException != null) |
241 |
{ |
242 |
App.logger.Error($"ExtractFile Error InnerException", ex.InnerException); |
243 |
} |
244 |
} |
245 |
finally |
246 |
{ |
247 |
Application.Current.Shutdown(); |
248 |
} |
249 |
} |
250 |
|
251 |
private static bool FileExits(string fileName) |
252 |
{ |
253 |
try |
254 |
{ |
255 |
return File.Exists(fileName); |
256 |
} |
257 |
catch (Exception) |
258 |
{ |
259 |
return false; |
260 |
} |
261 |
} |
262 |
|
263 |
private void ExtractProcess(AppCastItem item, string downloadPath) |
264 |
{ |
265 |
try |
266 |
{ |
267 |
downloadFile = downloadPath; |
268 |
|
269 |
//var args = $"{InstallPath} {tempStoragePath} {downloadFile} {KcomPath} {App.KcomParam}"; |
270 |
|
271 |
string endpoint = IIpc.Commons.shortGuid(); |
272 |
|
273 |
var wcfServer = new IIpc.WcfServer(endpoint + "M"); |
274 |
wcfServer.Start(); |
275 |
|
276 |
wcfServer.ProcessStart += (snd, ect) => |
277 |
{ |
278 |
var ipcClient = new IIpc.WcfClient(endpoint + "E"); |
279 |
try |
280 |
{ |
281 |
ipcClient.ParamReceived(InstallPath, tempStoragePath, downloadFile, KcomPath, App.KcomParam); |
282 |
|
283 |
//Application.Current.Shutdown(); |
284 |
} |
285 |
catch (Exception ex) |
286 |
{ |
287 |
App.logger.Error($"ParamReceived ", ex); |
288 |
} |
289 |
}; |
290 |
|
291 |
//MarkusExtract.GetFile(); |
292 |
|
293 |
var exrtractFile = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "MarkusExtractUpdate", @"MarkusUpdateExtract.exe"); |
294 |
|
295 |
Process DownloadProcess = new Process(); |
296 |
|
297 |
ProcessStartInfo startInfo = new ProcessStartInfo |
298 |
{ |
299 |
UseShellExecute = false, |
300 |
FileName = exrtractFile, |
301 |
WindowStyle = ProcessWindowStyle.Hidden, |
302 |
CreateNoWindow = true, |
303 |
ErrorDialog = false, |
304 |
Verb = "runas", |
305 |
RedirectStandardInput = false, |
306 |
RedirectStandardError = false, |
307 |
Arguments = endpoint |
308 |
}; |
309 |
|
310 |
//$"{ InstallPath} {tempStoragePath} {downloadFile} {KcomPath} {param}" |
311 |
|
312 |
DownloadProcess.StartInfo = startInfo; |
313 |
DownloadProcess.EnableRaisingEvents = false; |
314 |
|
315 |
DownloadProcess.Start(); |
316 |
} |
317 |
catch (Exception ex) |
318 |
{ |
319 |
App.logger.Error($"DownloadFileIsReady ", ex); |
320 |
} |
321 |
|
322 |
} |
323 |
} |
324 |
} |