Пример #1
0
        public static bool IsUpdateNeeded()
        {
            try
            {
                string          currentExePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
                string          exeName        = Path.GetFileName(currentExePath);
                string          updateExePath  = Path.Combine(MQueryCommand.GetSetting("MonitorSelfUpdatePath").Value, exeName);
                FileVersionInfo currentFileVer = FileVersionInfo.GetVersionInfo(currentExePath);
                Version         currentVersion = new Version(currentFileVer.FileVersion);
                FileVersionInfo updateFileVer  = FileVersionInfo.GetVersionInfo(updateExePath);
                Version         updateVersion  = new Version(updateFileVer.FileVersion);

                return(!CompareFileVersion(currentVersion, updateVersion));
            }
            catch (Exception ex)
            {
                MLogger.Error(ex.ToString());
            }
            return(false);
        }
Пример #2
0
        public static void Update(Form updatingForm = null)
        {
            ShowDialogAsync();

            const string SETTINGS_JSON = "settings.json";

            string          currentExePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
            string          exeName        = Path.GetFileName(currentExePath);
            string          updateExePath  = Path.Combine(MQueryCommand.GetSetting("MonitorSelfUpdatePath").Value, exeName);
            FileVersionInfo currentFileVer = FileVersionInfo.GetVersionInfo(currentExePath);
            Version         currentVersion = new Version(currentFileVer.FileVersion);
            FileVersionInfo updateFileVer  = FileVersionInfo.GetVersionInfo(updateExePath);
            Version         updateVersion  = new Version(updateFileVer.FileVersion);

            try
            {
                if (File.Exists(updateExePath))
                {
                    string currentPath  = Path.GetDirectoryName(currentExePath);
                    string updatingPath = Path.GetDirectoryName(updateExePath);

                    string[] allCurrentOtherFiles = GetFiles(currentPath, new string[] { "*.*_" }, SearchOption.TopDirectoryOnly);

                    try
                    {
                        foreach (var currentFile in allCurrentOtherFiles)
                        {
                            WaitFileUnlocking(currentFile, 10, 2000);
                            File.Delete(currentFile);
                        }
                    }
                    catch (Exception ex)
                    {
                        MLogger.Debug(ex.Message);
                    }

                    File.Move(currentExePath, currentExePath + "_");

                    //copy standard file
                    if (File.Exists(updateExePath) && !File.Exists(currentExePath))
                    {
                        File.Copy(updateExePath, currentExePath, true);
                    }
                    else
                    {
                        MLogger.Debug("невозможно скопировать .exe - файл MagicUpdaterMonitor'а, либо не переименовался текущий файл, либо не существует файла для обновления.");
                    }

                    // Подменяем библиотеки и прочие файлы
                    string[] searchPatterns = new string[]
                    {
                        "*.dll",
                        "*.xml",
                        "*.config",
                        "*.pdb",
                        "*.manifest",
                    };

                    string[] currentOtherFiles  = GetFiles(currentPath, searchPatterns, SearchOption.TopDirectoryOnly);
                    string[] updatingOtherFiles = GetFiles(updatingPath, searchPatterns, SearchOption.TopDirectoryOnly);

                    try
                    {
                        foreach (var currentFile in currentOtherFiles)
                        {
                            File.Move(currentFile, $"{currentFile}_");
                        }
                    }
                    catch (Exception ex)
                    {
                        MLogger.Debug(ex.Message);
                    }

                    try
                    {
                        File.Copy(Path.Combine(updatingPath, SETTINGS_JSON), Path.Combine(updatingPath, SETTINGS_JSON).Replace(updatingPath, currentPath), true);
                    }
                    catch (Exception ex)
                    {
                        MLogger.Debug(ex.Message);
                    }

                    try
                    {
                        foreach (var updatingFile in updatingOtherFiles)
                        {
                            File.Copy(updatingFile, updatingFile.Replace(updatingPath, currentPath), true);
                        }
                    }
                    catch (Exception ex)
                    {
                        MLogger.Debug(ex.Message);
                    }

                    bool isProcessCreated = false;
                    for (int i = 3; i >= 0; i--)
                    {
                        //execute copied (standard) file
                        Process process = null;
                        if (!isProcessCreated)
                        {
                            process = Process.Start(currentExePath);
                        }
                        Thread.Sleep(2000);
                        isProcessCreated = process != null && !process.HasExited;
                        if (isProcessCreated)
                        {
                            break;
                        }
                    }

                    if (!isProcessCreated)
                    {
                        MLogger.Debug("не удалось запустить обновленную версию MagicUpdaterMonitor'а");
                    }
                    else
                    {
                        CloseAsync();
                        Harakiri();
                    }
                }
                else
                {
                    MLogger.Debug(".exe - файл MagicUpdaterMonitor'а не найден.");
                }
            }
            catch (Exception ex)
            {
                MLogger.Debug(ex.Message);
                CloseAsync();
                Harakiri();
            }
        }
Пример #3
0
        public static bool UpdateNew()
        {
            #region Initialization
            _restartAppCurrentExePath   = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), Constants.RESTART_MONITOR_APPLICATION_FOLDER, Constants.RESTART_MONITOR_APPLICATION_EXE_NAME);
            _restartAppUpdateExePath    = Path.Combine(MQueryCommand.GetSetting("MonitorSelfUpdatePath").Value, Constants.RESTART_MONITOR_APPLICATION_FOLDER, Constants.RESTART_MONITOR_APPLICATION_EXE_NAME);
            _restartAppUpdatePath       = Path.Combine(MQueryCommand.GetSetting("MonitorSelfUpdatePath").Value, Constants.RESTART_MONITOR_APPLICATION_FOLDER);
            _restartAppCurrenPath       = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), Constants.RESTART_MONITOR_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);
            }

            _restartAppUpdateExeVersion = new Version(FileVersionInfo.GetVersionInfo(_restartAppUpdateExePath).FileVersion);
            #endregion Initialization

            #region RestartApplicationUpdate
            //Мутим 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)
            {
                MLogger.Error($"Ошибка создания бекапа текущей версии {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)
            {
                MLogger.Error($"Ошибка удаления текущей версии. 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)
            {
                MLogger.Error($"Ошибка копирования новой версии в попку текущей. Original: {ex.Message}. Выполняется восстановление из бекапа");
                RestoreCurrentVersionFromBackup();
                return(false);
            }
            #endregion RestartApplicationUpdate

            return(true);
        }