示例#1
0
        private async Task StartDownloadAndInstall()
        {
            foreach (var item in StartupParams.Instance.LastVersionList)
            {
                _currentInstallVersionName = item.VersionName;
                UpdateControlState(() =>
                {
                    ChangeLog.Text = item.ChangeLog;
                });
                UpdateControlState(() =>
                {
                    DownloadVersionText.Text = $"正在下载{_currentInstallVersionName}版本的更新包";
                });
                _webClient = new DownloadWebClient {
                    CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore)
                };
                _tempFile = Path.GetTempFileName();
                var uri = new Uri(item.DownloadUrl);
                _webClient.DownloadProgressChanged += _webClient_DownloadProgressChanged;;

                _webClient.DownloadFileCompleted += WebClient_DownloadFileCompleted;

                await _webClient.DownloadFileTaskAsync(uri, _tempFile);

                SaveUpdateVersionInfo(item.Version, item.VersionName);
            }
        }
示例#2
0
        private void WebClient_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            UpdateControlState(() =>
            {
                DownloadVersionText.Text = $"正在安装{_currentInstallVersionName}版本的更新包";
            });
            string fileName;
            string contentDisposition = _webClient.ResponseHeaders["Content-Disposition"] ?? string.Empty;

            if (string.IsNullOrEmpty(contentDisposition))
            {
                fileName = Path.GetFileName(_webClient.ResponseUri.LocalPath);
            }
            else
            {
                fileName = _tryToFindFileName(contentDisposition, "filename=");
                if (string.IsNullOrEmpty(fileName))
                {
                    fileName = _tryToFindFileName(contentDisposition, "filename*=UTF-8''");
                }
            }
            var tempPath = Path.Combine(Path.GetTempPath(), fileName);

            try
            {
                if (File.Exists(tempPath))
                {
                    File.Delete(tempPath);
                }
                File.Move(_tempFile, tempPath);
            }
            catch (Exception ex)
            {
                UpdateControlState(() =>
                {
                    DownloadVersionText.Text = $"安装{_currentInstallVersionName}版本的更新包失败," + ex.Message;
                });
                _webClient = null;
                return;
            }
            var path = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);

            ZipStorer zip = ZipStorer.Open(tempPath, FileAccess.Read);

            List <ZipStorer.ZipFileEntry> dir = zip.ReadCentralDir();

            for (var index = 0; index < dir.Count; index++)
            {
                ZipStorer.ZipFileEntry entry = dir[index];
                zip.ExtractFile(entry, Path.Combine(path, entry.FilenameInZip));
                int progress = (index + 1) * 100 / dir.Count;;
                UpdateControlState(() =>
                {
                    DownloadProgressBar.Value = progress;
                });
                UpdateControlState(() =>
                {
                    DownloadProgressText.Text = progress + "%";
                });
            }
            zip.Close();

            UpdateControlState(() =>
            {
                DownloadVersionText.Text = $"版本{_currentInstallVersionName}安装成功";
            });
            UpdateControlState(() =>
            {
                DownloadProgressText.Text = string.Empty;
            });
            UpdateControlState(() =>
            {
                CurrentVersion.Content = _currentInstallVersionName;
            });
        }