private async void MainWindowOnLoaded(object sender, RoutedEventArgs e) { var signedManifest = await TryObtainManifest(); if (null == signedManifest) { return; } // Удаляем, если уже установлено. if (Directory.Exists(SetupConsts.AppDirectory)) { var message = string.Format(CultureInfo.InvariantCulture, AppResources .MainWindow_MainWindowOnLoaded_The_program_is_already_installed_on_your_computer__Would_you_like_to_replace_it_with_version__0__of__1__, signedManifest.Version, signedManifest.Date.ToLocalTime().ToShortDateString()); if (MessageBoxResult.No == MessageBox.Show( message, AppResources.MainWindow_MainWindowOnLoaded_The_program_is_already_installed, MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.Yes)) { Close(); return; } } if (Directory.Exists(SetupConsts.AppDirectory)) { try { Directory.Delete(SetupConsts.AppDirectory, true); } catch (Exception exception) { Cancel( AppResources .MainWindow_MainWindowOnLoaded_Unable_to_remove_the_previous_installation_, exception.Message); return; } } _doNotTouch = false; try { Directory.CreateDirectory(SetupConsts.AppDirectory); } catch (Exception exception) { Cancel(AppResources.MainWindow_MainWindowOnLoaded_Failed_to_create_directory_, exception.Message); return; } // Скачиваем. InfoLabel.Text = string.Format(CultureInfo.CurrentCulture, AppResources.MainWindow_MainWindowOnLoaded_Download_version__0__of__1, signedManifest.Version, signedManifest.Date.ToLocalTime().ToString("D")); SetPercentage(0); _tempFilePath = Path.GetTempFileName(); try { await _webClient.DownloadFileAsyncTask(new Uri(signedManifest.PackageUrl), _tempFilePath); } catch (TaskCanceledException) { Close(); return; } catch (Exception exception) { Cancel(AppResources.MainWindow_MainWindowOnLoaded_Unable_to_download_file, exception.Message); return; } // Проверяем хеш. byte[] expectedHash = HashUtility.FromHexString(signedManifest.Digest); var hash = HashUtility.ComputeHash(_tempFilePath); if (!expectedHash.SequenceEqual(hash)) { Cancel(AppResources.MainWindow_MainWindowOnLoaded_The_file_is_corrupted); return; } InfoLabel.Text = AppResources.MainWindow_MainWindowOnLoaded_File_successfully_downloaded; SetPercentage(100); await Delay(500); // Распаковываем архив и добавляем ярлыки. InfoLabel.Text = AppResources.MainWindow_MainWindowOnLoaded_Archive_unpacking___; PercentageLabel.Content = string.Empty; ProgressBar.IsIndeterminate = true; await Delay(500); _unzipTask = Task.Factory.StartNew(() => { ExtractToDirectory(_tempFilePath, SetupConsts.AppDirectory); File.Delete(_tempFilePath); File.WriteAllBytes(Path.Combine(SetupConsts.AppDirectory, SetupConsts.UninstallAssistantFile), AppResources.Uninstall); var ruDirectory = Path.Combine(SetupConsts.AppDirectory, "ru"); if (!Directory.Exists(ruDirectory)) { Directory.CreateDirectory(ruDirectory); } File.WriteAllBytes(Path.Combine(ruDirectory, SetupConsts.UninstallAssistantResourcesFile), AppResources.Uninstall_resources); var exePath = Path.Combine(SetupConsts.AppDirectory, SetupConsts.AppStartupFile); ShortcutUtility.CreateShortcut(exePath, Environment.GetFolderPath(Environment.SpecialFolder.StartMenu)); ShortcutUtility.CreateShortcut(exePath, Environment.GetFolderPath(Environment.SpecialFolder.Desktop)); var size = CalculateDirectorySize(new DirectoryInfo(SetupConsts.AppDirectory)); RegistryUtility.AddUninstallRegistryKey(size / 1024, signedManifest.Version); }, _unzipCancellationToken); try { await _unzipTask; } catch (TaskCanceledException) { Close(); return; } catch (Exception exception) { Cancel(AppResources.MainWindow_MainWindowOnLoaded_Unable_to_unzip_the_archive_, exception.Message); return; } _success = true; ProgressBar.IsIndeterminate = false; SetPercentage(100); InfoLabel.Text = AppResources .MainWindow_MainWindowOnLoaded_Program_installation_completed_successfully_; CancelButton.Content = AppResources.MainWindow_MainWindowOnLoaded_Finish; }