private static void CopyNewVersionFiles() { //Мутим Backup try { FilesWorks.DeleteDirectoryFull(_currentVersionBackupPath); Directory.CreateDirectory(_currentVersionBackupPath); if (Directory.Exists(_currentVersionBackupPath)) { foreach (var file in FilesWorks.GetFiles(_currentVersionPath, _searchPatterns, SearchOption.TopDirectoryOnly)) { file.CopyTo(Path.Combine(_currentVersionBackupPath, file.Name), true); } } } catch (Exception ex) { NLogger.LogErrorToHdd($"Ошибка создания бекапа текущей версии {MainSettings.Constants.MAGIC_UPDATER}. Original: {ex.Message}", MainSettings.Constants.MAGIC_UPDATER_RESTART); return; } //Удаляем текущую версию try { foreach (var file in FilesWorks.GetFiles(_currentVersionPath, _searchPatterns, SearchOption.TopDirectoryOnly)) { if (file.Name != "settings.json") { file.Delete(); } } } catch (Exception ex) { NLogger.LogErrorToHdd($"Ошибка удаления текущей версии. Original: {ex.Message}", MainSettings.Constants.MAGIC_UPDATER_RESTART); RestoreCurrentVersionFromBackup(); return; } //Копируем новую версию try { foreach (var file in FilesWorks.GetFiles(_newVersionPath, _searchPatterns, SearchOption.TopDirectoryOnly)) { file.CopyTo(Path.Combine(_currentVersionPath, file.Name)); } } catch (Exception ex) { NLogger.LogErrorToHdd($"Ошибка копирования новой версии в попку текущей. Original: {ex.Message}", MainSettings.Constants.MAGIC_UPDATER_RESTART); RestoreCurrentVersionFromBackup(); return; } }
public void Uninstall() { try { UninstallBase(); if (Directory.Exists(MAGIC_UPDATER_PATH)) { FilesWorks.DeleteDirectoryFull(MAGIC_UPDATER_PATH); } if (Directory.Exists(MAGIC_UPDATER_NEW_VER_PATH)) { FilesWorks.DeleteDirectoryFull(MAGIC_UPDATER_NEW_VER_PATH); } } catch (Exception ex) { Program.MainForm?.LogInstallServiceString(ex); AddMessage(ex.Message); NLogger.LogErrorToHdd(ex.ToString()); } }
private bool UpdateMyself() { //Убиваем все процессы MagicUpdaterRestart Tools.KillAllProcessByname(MainSettings.Constants.MAGIC_UPDATER_RESTART, true); #region Initialization _restartAppCurrentExePath = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), MainSettings.Constants.RESTART_SERVICE_APPLICATION_FOLDER, MainSettings.Constants.RESTART_SERVICE_APPLICATION_EXE_NAME); _restartAppUpdateExePath = Path.Combine(MainSettings.LocalSqlSettings.SelfUpdatePath, MainSettings.Constants.RESTART_SERVICE_APPLICATION_FOLDER, MainSettings.Constants.RESTART_SERVICE_APPLICATION_EXE_NAME); _restartAppUpdatePath = Path.Combine(MainSettings.LocalSqlSettings.SelfUpdatePath, MainSettings.Constants.RESTART_SERVICE_APPLICATION_FOLDER); _restartAppCurrenPath = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), MainSettings.Constants.RESTART_SERVICE_APPLICATION_FOLDER); _restartAppCurrenPathBackup = Path.Combine(_restartAppCurrenPath, "backup"); _searchPatterns = new string[] { "*.exe", "*.dll", "*.xml", "*.config", "*.pdb", "*.manifest", }; if (!File.Exists(_restartAppCurrentExePath)) { _restartAppCurrentExeVersion = new Version(0, 0, 0, 0); } else { _restartAppCurrentExeVersion = new Version(FileVersionInfo.GetVersionInfo(_restartAppCurrentExePath).FileVersion); } if (!File.Exists(_restartAppUpdateExePath)) { AppendLogMessage($"Отсутствует файл по пути \"{_restartAppUpdateExePath}\""); return(false); } _restartAppUpdateExeVersion = new Version(FileVersionInfo.GetVersionInfo(_restartAppUpdateExePath).FileVersion); #endregion Initialization #region RestartApplicationUpdate if (!CompareFileVersion(_restartAppCurrentExeVersion, _restartAppUpdateExeVersion)) { //Мутим Backup try { FilesWorks.DeleteDirectoryFull(_restartAppCurrenPathBackup); Directory.CreateDirectory(_restartAppCurrenPathBackup); if (Directory.Exists(_restartAppCurrenPathBackup)) { foreach (var file in FilesWorks.GetFiles(_restartAppCurrenPath, _searchPatterns, SearchOption.TopDirectoryOnly)) { file.CopyTo(Path.Combine(_restartAppCurrenPathBackup, file.Name)); } } } catch (Exception ex) { AppendLogMessage($"Ошибка создания бекапа текущей версии {MainSettings.Constants.MAGIC_UPDATER_RESTART}. Original: {ex.Message}"); return(false); } //Удаляем текущую версию try { foreach (var file in FilesWorks.GetFiles(_restartAppCurrenPath, _searchPatterns, SearchOption.TopDirectoryOnly)) { file.Delete(); } } catch (Exception ex) { AppendLogMessage($"Ошибка удаления текущей версии. Original: {ex.Message}. Выполняется восстановление из бекапа"); RestoreCurrentVersionFromBackup(); return(false); } //Копируем новую версию try { Directory.CreateDirectory(_restartAppCurrenPath); foreach (var file in FilesWorks.GetFiles(_restartAppUpdatePath, _searchPatterns, SearchOption.TopDirectoryOnly)) { file.CopyTo(Path.Combine(_restartAppCurrenPath, file.Name)); } } catch (Exception ex) { AppendLogMessage($"Ошибка копирования новой версии в попку текущей. Original: {ex.Message}. Выполняется восстановление из бекапа"); RestoreCurrentVersionFromBackup(); return(false); } } #endregion RestartApplicationUpdate IsSendLogAndStatusAfterExecution = false; Tools.SelfUpdateAndRestart(Id); return(true); }