Beispiel #1
0
        internal static UpdateAttempt CheckForPreviousAttempts(string tempFolderName, Version currentVersion, Version newVersion)
        {
            var           attemptPath = GetUniformPath(tempFolderName, _attemptFileName);
            UpdateAttempt attempt;

            if (System.IO.File.Exists(attemptPath))
            {
                attempt = JsonConvert.DeserializeObject <UpdateAttempt>(System.IO.File.ReadAllText(attemptPath));
            }
            else
            {
                attempt = new UpdateAttempt {
                    Date = DateTime.Now, Version = newVersion
                };
                var attemptJson = JsonConvert.SerializeObject(attempt);
                System.IO.File.WriteAllText(attemptPath, attemptJson);
            }

            return(attempt);
        }
Beispiel #2
0
        /// <summary>
        /// Applies the update after it has been downloaded. Must be called after calling DownloadLatestUpdate.
        /// </summary>
        /// <param name="tempFolderPath">The path of the folder in which the update files are located.</param>
        public static void ApplyUpdate(string tempFolderPath)
        {
            tempFolderPath = Helper.GetUniformPath(tempFolderPath);

            UpdateAttempt attempt     = null;
            var           attemptPath = Helper.GetUniformPath(tempFolderPath, Helper._attemptFileName);

            if (System.IO.File.Exists(attemptPath))
            {
                var attemptJson = System.IO.File.ReadAllText(attemptPath);
                attempt = JsonConvert.DeserializeObject <UpdateAttempt>(attemptJson);
            }

            var    appFolder  = Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]);
            string backupPath = Helper.BackUpAppFiles(appFolder);

            try
            {
                var downloadedFiles = new DirectoryInfo(tempFolderPath).EnumerateFiles("*.*", SearchOption.AllDirectories)
                                      .Select(info => Helper.GetRelativePath(tempFolderPath, Helper.GetUniformPath(info.FullName)));

                var blackList = downloadedFiles.Where(p => System.IO.File.Exists(Helper.GetUniformPath(appFolder, p)))
                                .Union(attempt?.FilesToDelete);

                foreach (var item in blackList)
                {
                    if (!System.IO.File.Exists(item))
                    {
                        continue;
                    }

                    var newFileName = item + ".bak";
                    if (System.IO.File.Exists(newFileName))
                    {
                        System.IO.File.Delete(newFileName);
                    }
                    System.IO.File.Move(item, newFileName);
                }

                foreach (var file in downloadedFiles)
                {
                    var tempPath = Helper.GetUniformPath(tempFolderPath, file);
                    if (System.IO.File.Exists(tempPath))
                    {
                        var newFileName = Helper.GetUniformPath(appFolder, file);
                        Helper.FileCopy(tempPath, newFileName);
                    }
                    else if (Directory.Exists(tempPath))
                    {
                        var newDirName = Helper.GetUniformPath(appFolder, file);
                        Helper.DirectoryCopy(tempPath, newDirName);
                    }
                }
            }
            catch (Exception)
            {
                Helper.RollBackChanges(backupPath, appFolder);
                throw;
            }
            finally
            {
                Directory.Delete(backupPath, true);
                Directory.Delete(tempFolderPath, true);
            }
        }