示例#1
0
 /// <summary>
 /// Creates a new instance of type UpdateCheckEventArgs.
 /// </summary>
 public UpdateCheckEventArgs(bool success, AppUpdate update, UpdateNotifyMode notifyMode, Exception ex = null)
 {
     Successful = success;
     Update     = update;
     if (update != null && ex == null)
     {
         NewVersion = new Version(update.Version) > new Version(AppInfo.Version);
     }
     UpdateNotifyMode     = notifyMode;
     UpdateCheckException = ex;
 }
示例#2
0
        /// <summary>
        /// Downloads the file specified in the given update and verifies the hash sum if available.
        /// </summary>
        /// <param name="update">The update to be downloaded.</param>
        /// <param name="downloadProgress">A download progress provider.</param>
        /// <param name="ct">A CancellationToken used to cancel the download.</param>
        /// <returns>The full path to the downloaded file if the download was successful; null if the download was canceled.</returns>
        /// <exception cref="UpdateFailedException">If downloading the download failed.</exception>
        public async Task <string> DownloadUpdate(AppUpdate update, IProgress <int> downloadProgress = null,
                                                  CancellationToken ct = default(CancellationToken))
        {
            DownloadEntry entry    = ResolveDownloadEntry(update);
            Uri           uri      = new Uri(entry.Link, UriKind.RelativeOrAbsolute);
            string        filePath = Path.Combine(Path.GetTempPath(), entry.FileName);

            try
            {
                if (uri.IsAbsoluteUri)
                {
                    WebClient client = new WebClient();
                    ct.Register(client.CancelAsync);
                    await client.DownloadFileTaskAsync(new Uri(entry.Link, UriKind.RelativeOrAbsolute), filePath, downloadProgress);
                }
                else
                {
                    File.Copy(uri.ToString(), filePath, true);
                }
                Debug.WriteLine(String.Format("Downloaded update to {0}", filePath));
            }
            catch (WebException ex) when(ex.Message == "The request was aborted: The request was canceled.")
            {
                // The download was cancelled by the user. Don't throw an exception.
                return(null);
            }
            catch (Exception e)
            {
                throw new UpdateFailedException("Downloading the update failed.", e);
            }
            if (entry.FileHash != null)
            {
                if (VerifyHash(entry.FileHash, filePath))
                {
                    return(filePath);
                }
                else
                {
                    File.Delete(filePath);
                    throw new UpdateFailedException("File verification failed.");
                }
            }
            else
            {
                return(filePath);
            }
        }
示例#3
0
        /// <summary>
        /// Resolves the correct download information based on the DownloadIdentifier of the update checker.
        /// </summary>
        public DownloadEntry ResolveDownloadEntry(AppUpdate update)
        {
            DownloadEntry entry = null;

            if (DownloadIdentifier != null)
            {
                entry = update.Downloads?.FirstOrDefault(de => de.Key == DownloadIdentifier);
            }
            // construct a download entry from default values if key was not found.
            if (entry == null)
            {
                entry = new DownloadEntry()
                {
                    Link     = update.DownloadLink,
                    FileName = update.DownloadFileName,
                    FileHash = null
                };
            }
            if (entry.FileName == null)
            {
                entry.FileName = getFileName(entry.Link);
            }
            return(entry);
        }