private void Window_Loaded(object sender, RoutedEventArgs e) { _webClient = new EasyUpdaterWebClient { CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore) }; var uri = new Uri(this._eazyUpdate.UpdaterInformation.DownloadURL); if (string.IsNullOrEmpty(this._eazyUpdate.UpdaterOptions.DownloadPath)) { _tempFile = Path.GetTempFileName(); } else { _tempFile = Path.Combine(this._eazyUpdate.UpdaterOptions.DownloadPath, $"{Guid.NewGuid().ToString()}.tmp"); if (!Directory.Exists(this._eazyUpdate.UpdaterOptions.DownloadPath)) { Directory.CreateDirectory(this._eazyUpdate.UpdaterOptions.DownloadPath); } } if (this._eazyUpdate.UpdaterOptions.DownloadAuthentication != null) { _webClient.Headers[HttpRequestHeader.Authorization] = this._eazyUpdate.UpdaterOptions.DownloadAuthentication.ToString(); } _webClient.DownloadProgressChanged += OnDownloadProgressChanged; _webClient.DownloadFileCompleted += WebClientOnDownloadFileCompleted; _webClient.DownloadFileAsync(uri, _tempFile); }
/// <summary> /// Web client file download completed event /// </summary> /// <param name="sender">The sender object (webclient)</param> /// <param name="e">The async completed event arg</param> private void WebClientOnDownloadFileCompleted(object sender, AsyncCompletedEventArgs e) { if (e.Cancelled) { return; } if (e.Error != null) { MessageBox.Show(e.Error.Message, e.Error.GetType().ToString(), MessageBoxButton.OK, MessageBoxImage.Error); _webClient = null; Close(); return; } if (!string.IsNullOrEmpty(_eazyUpdate.UpdaterInformation.Checksum)) { if (!CompareChecksum(_tempFile, _eazyUpdate.UpdaterInformation.Checksum)) { _webClient = null; Close(); return; } } string fileName; string contentDisposition = _webClient.ResponseHeaders["Content-Disposition"] ?? string.Empty; if (string.IsNullOrEmpty(contentDisposition)) { fileName = Path.GetFileName(_webClient.ResponseUri.LocalPath); } else { fileName = TryToFindFileName(contentDisposition, "filename="); if (string.IsNullOrEmpty(fileName)) { fileName = TryToFindFileName(contentDisposition, "filename*=UTF-8''"); } } var tempPath = Path.Combine( string.IsNullOrEmpty(_eazyUpdate.UpdaterOptions.DownloadPath) ? Path.GetTempPath() : _eazyUpdate.UpdaterOptions.DownloadPath, fileName); try { if (File.Exists(tempPath)) { File.Delete(tempPath); } File.Move(_tempFile, tempPath); } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString(), MessageBoxButton.OK, MessageBoxImage.Error); _webClient = null; Close(); return; } var processStartInfo = new ProcessStartInfo { FileName = tempPath, UseShellExecute = true, Arguments = _eazyUpdate.UpdaterInformation.InstallerArgs?.Replace("%path%", Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName)) }; var extension = Path.GetExtension(tempPath); if (extension.Equals(".msi", StringComparison.OrdinalIgnoreCase)) { processStartInfo = new ProcessStartInfo { FileName = "msiexec", UseShellExecute = false, Arguments = $"/i \"{tempPath}\"" }; } else if (extension.Equals(".exe", StringComparison.OrdinalIgnoreCase)) { processStartInfo = new ProcessStartInfo { FileName = tempPath }; } if (!string.IsNullOrEmpty(_eazyUpdate.UpdaterInformation.InstallerArgs)) { processStartInfo.Arguments += " " + _eazyUpdate.UpdaterInformation.InstallerArgs; } if (_eazyUpdate.UpdaterOptions.RunUpdateAsAdmin) { processStartInfo.Verb = "runas"; } if (_eazyUpdate.UpdaterOptions.RunUpdateAsAnotherUser && _eazyUpdate.UpdaterOptions.ServiceAccount != null) { SecureString ssPwd = new SecureString(); Array.ForEach(_eazyUpdate.UpdaterOptions.ServiceAccount.Password.ToCharArray(), (x) => ssPwd.AppendChar(x)); ssPwd.MakeReadOnly(); processStartInfo.Domain = _eazyUpdate.UpdaterOptions.ServiceAccount.Domain; processStartInfo.UserName = _eazyUpdate.UpdaterOptions.ServiceAccount.UserName; processStartInfo.Password = ssPwd; } try { Process.Start(processStartInfo); } catch (Win32Exception exception) { _webClient = null; if (exception.NativeErrorCode != 1223) { throw; } } Close(); }