/// <summary>
        /// Compare check sum of the downloaded update file with the algorithm and check sum given
        /// </summary>
        /// <param name="fileName">The downloaded file name</param>
        /// <param name="checksum">The check sum value</param>
        /// <returns>Returns true if it matches otherwise false</returns>
        private bool CompareChecksum(string fileName, string checksum)
        {
            using (var hashAlgorithm = HashAlgorithm.Create(this._eazyUpdate.UpdaterInformation.HashingAlgorithm))
            {
                using (var stream = File.OpenRead(fileName))
                {
                    if (hashAlgorithm != null)
                    {
                        var hash         = hashAlgorithm.ComputeHash(stream);
                        var fileChecksum = BitConverter.ToString(hash).Replace("-", string.Empty).ToLowerInvariant();

                        if (fileChecksum == checksum.ToLower())
                        {
                            return(true);
                        }

                        ActionInvokeEnabler.InvokeIfNecessary(() =>
                                                              MessageBox.Show(ResourceEnabler.GetResourceText("FileIntegrityCheckFailedMessage"),
                                                                              ResourceEnabler.GetResourceText("FileIntegrityCheckFailedCaption"),
                                                                              MessageBoxButton.OK, MessageBoxImage.Error));
                    }
                    else
                    {
                        if (this._eazyUpdate.UpdaterOptions.ReportErrors)
                        {
                            ActionInvokeEnabler.InvokeIfNecessary(() =>
                                                                  MessageBox.Show(ResourceEnabler.GetResourceText("UnsupportedHashAlgorithmMessage"),
                                                                                  ResourceEnabler.GetResourceText("UnsupportedHashAlgorithmCaption"),
                                                                                  MessageBoxButton.OK, MessageBoxImage.Error));
                        }
                    }

                    return(false);
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Run application update with current update information
        /// </summary>
        /// <returns>Empty Task object that can be awaited</returns>
        private Task RunApplicationUpdate()
        {
            if (this.UpdaterInformation.CurrentVersion == null ||
                string.IsNullOrEmpty(this.UpdaterInformation.DownloadURL))
            {
                this.IsUpdaterRunning = false;
                throw new InvalidDataException();
            }

            if (!this.UpdaterInformation.IsMandatoryUpdate)
            {
                var registryValues = this.EazyUpdateReadWriter.GetRegistryValues(this.RegistryLocation,
                                                                                 new Collection <string> {
                    RegistryKeys.Skip, RegistryKeys.Version, RegistryKeys.RemindLater
                });

                if (registryValues?.Any() == true)
                {
                    if (registryValues[RegistryKeys.Skip] != null && registryValues[RegistryKeys.Version] != null)
                    {
                        string skipValue   = registryValues[RegistryKeys.Skip].ToString();
                        var    skipVersion = new Version(registryValues[RegistryKeys.Version].ToString());
                        if (skipValue.Equals("1") && this.UpdaterInformation.CurrentVersion <= skipVersion)
                        {
                            return(null);
                        }

                        if (this.UpdaterInformation.CurrentVersion > skipVersion)
                        {
                            var newRegistryValue = new Dictionary <string, object>
                            {
                                { RegistryKeys.Version, this.UpdaterInformation.CurrentVersion.ToString() },
                                { RegistryKeys.Skip, 0 }
                            };

                            this.EazyUpdateReadWriter.UpdateRegistry(this.RegistryLocation, newRegistryValue);
                        }
                    }

                    var remindLaterTime = registryValues?[RegistryKeys.RemindLater];

                    if (remindLaterTime != null)
                    {
                        DateTime remindLater = Convert.ToDateTime(remindLaterTime.ToString(),
                                                                  CultureInfo.CreateSpecificCulture("en-US").DateTimeFormat);

                        int compareResult = DateTime.Compare(DateTime.Now, remindLater);

                        if (compareResult < 0)
                        {
                            this.SetTimer(remindLater);
                            return(null);
                        }
                    }
                }
            }

            if (this.CustomNotification != null)
            {
                this.CustomNotification?.Invoke(this, new NotificationEventArgs(this.UpdaterInformation));
            }
            else
            {
                if (this.UpdaterInformation != null)
                {
                    if (this.UpdaterInformation.IsUpdateAvailable)
                    {
                        if (!this.IsWinFormsApplication)
                        {
                            System.Windows.Forms.Application.EnableVisualStyles();
                        }

                        if (this.UpdaterInformation.IsMandatoryUpdate &&
                            this.UpdaterInformation.UpdateMode == EazyUpdateMode.ForcedDownload)
                        {
                            this.DownloadUpdate();
                            this.Exit();
                        }
                        else
                        {
                            if (Thread.CurrentThread.GetApartmentState().Equals(ApartmentState.STA))
                            {
                                this.ShowUpdateForm();
                            }
                            else
                            {
                                Thread thread = new Thread(this.ShowUpdateForm);
                                thread.CurrentCulture = thread.CurrentUICulture = CultureInfo.CurrentCulture;
                                thread.SetApartmentState(ApartmentState.STA);
                                thread.Start();
                                thread.Join();
                            }
                        }

                        return(null);
                    }
                    else
                    {
                        if (this.UpdaterOptions.ReportErrors)
                        {
                            ActionInvokeEnabler.InvokeIfNecessary(() =>
                                                                  System.Windows.MessageBox.Show(ResourceEnabler.GetResourceText("UpdateUnavailableMessage"),
                                                                                                 ResourceEnabler.GetResourceText("UpdateUnavailableCaption"),
                                                                                                 MessageBoxButton.OK, MessageBoxImage.Information));
                        }
                    }
                }
                else
                {
                    if (this.UpdaterOptions.ReportErrors)
                    {
                        ActionInvokeEnabler.InvokeIfNecessary(() =>
                                                              System.Windows.MessageBox.Show(ResourceEnabler.GetResourceText("UpdateCheckFailedMessage"),
                                                                                             ResourceEnabler.GetResourceText("UpdateCheckFailedCaption"),
                                                                                             MessageBoxButton.OK, MessageBoxImage.Information));
                    }
                }
            }

            this.IsUpdaterRunning = false;
            return(null);
        }