123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417 |
- using ProjectBase.Data.Logs;
- using ProjectBase.Data.Zip;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Xml;
- using System.Xml.Serialization;
- namespace ProjectBase.Data.Update
- {
- /// <summary>
- /// 程序更新事件参数
- /// </summary>
- public class ManifestEventArgs : EventArgs
- {
- public Manifest Manifest { get; set; }
- }
- /// <summary>
- /// 激活安装开始事件参数
- /// </summary>
- public class ActivationStartedEventArgs : EventArgs
- {
- public Manifest Manifest { get; set; }
- public bool Cancel { get; set; }
- }
- /// <summary>
- /// 安装完成事件参数
- /// </summary>
- public class ActivationCompletedEventArgs : AsyncCompletedEventArgs
- {
- public ActivationCompletedEventArgs(Exception error, bool cancelled, object userState)
- : base(error, cancelled, userState)
- {
- }
- public Manifest Manifest { get; set; }
- }
- /// <summary>
- /// 程序自动更新操作类,封装了文件下载、文件复制、文件解压等操作
- /// </summary>
- public class UpdateClass
- {
- #region 变量属性
- private DownloadClass downloader = new DownloadClass();
- private FileCopyClass fileCopyer = new FileCopyClass();
- private UpdaterConfigurationView updateCfgView = new UpdaterConfigurationView();
- private string backupFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../backup");
- /// <summary>
- /// 封装的文件下载操作类
- /// </summary>
- public DownloadClass Downloader
- {
- get
- {
- return downloader;
- }
- }
- /// <summary>
- /// 封装的文件复制操作类
- /// </summary>
- public FileCopyClass FileCopyer
- {
- get
- {
- return fileCopyer;
- }
- }
- #endregion
- #region 事件
- /// <summary>
- /// 下载进度
- /// </summary>
- public event EventHandler<DownloadProgressEventArgs> DownloadProgressChanged;
- /// <summary>
- /// 下载完成事件
- /// </summary>
- public event EventHandler<DownloadCompleteEventArgs> DownloadCompleted;
- /// <summary>
- /// 下载错误触发的事件
- /// </summary>
- public event EventHandler<DownloadErrorEventArgs> DownloadError;
- public event EventHandler<ManifestEventArgs> ActivationInitializing;
- public event EventHandler<ActivationCompletedEventArgs> ActivationCompleted;
- public event EventHandler<ActivationStartedEventArgs> ActivationStarted;
- public event EventHandler<FileCopyProgressChangedEventArgs> ActivationProgressChanged;
- public event EventHandler<FileCopyErrorEventArgs> ActivationError;
- #endregion
- #region 下载更新实现
- public UpdateClass()
- {
- downloader.DownloadCompleted += new EventHandler<DownloadCompleteEventArgs>(downloader_DownloadCompleted);
- downloader.DownloadError += new EventHandler<DownloadErrorEventArgs>(downloader_DownloadError);
- downloader.DownloadProgressChanged += new EventHandler<DownloadProgressEventArgs>(downloader_DownloadProgressChanged);
- fileCopyer.FileCopyError += new EventHandler<FileCopyErrorEventArgs>(fileCopyer_FileCopyError);
- fileCopyer.FileCopyCompleted += new EventHandler<FileCopyCompletedEventArgs>(fileCopyer_FileCopyCompleted);
- fileCopyer.FileCopyProgressChanged += new EventHandler<FileCopyProgressChangedEventArgs>(fileCopyer_FileCopyProgressChanged);
- }
- /// <summary>
- /// 是否有最新的版本
- /// </summary>
- public bool HasNewVersion
- {
- get
- {
- var m = CheckForUpdates();
- return m.Length > 0;
- }
- }
- /// <summary>
- /// 检查更新,返回更新清单列表
- /// </summary>
- /// <returns></returns>
- public Manifest[] CheckForUpdates()
- {
- updateCfgView.Refresh();
- Uri uri = new Uri(updateCfgView.ManifestUri);
- string doc = DownLoadFile(uri, updateCfgView.ManifestName, updateCfgView.ManifestPasswd);
- XmlSerializer xser = new XmlSerializer(typeof(Manifest));
- var manifest = xser.Deserialize(new XmlTextReader(doc, XmlNodeType.Document, null)) as Manifest;
- if (manifest == null ||
- manifest.Version == updateCfgView.Version ||
- manifest.MyApplication.ApplicationId != updateCfgView.ApplicationId)
- {
- return new Manifest[0];
- }
- return new Manifest[] { manifest };
- }
- /// <summary>
- /// 用于远程下载文件清单
- /// </summary>
- /// <param name="uri">文件清单网络路径</param>
- /// <returns></returns>
- private string DownLoadFile(Uri uri, string username, string password)
- {
- WebRequest request = WebRequest.Create(uri);
- request.Credentials = new NetworkCredential(username, password);
- string response = String.Empty;
- using (WebResponse res = request.GetResponse())
- {
- using (StreamReader reader = new StreamReader(res.GetResponseStream(), true))
- {
- response = reader.ReadToEnd();
- }
- }
- return response;
- }
- /// <summary>
- /// 同步下载文件清单中的文件
- /// </summary>
- /// <param name="manifests">下载文件清单</param>
- public void Download(Manifest[] manifests)
- {
- foreach (var m in manifests)
- {
- downloader.Download(m);
- }
- }
- /// <summary>
- /// 异步下载文件清单中的文件
- /// </summary>
- /// <param name="manifests">下载文件清单</param>
- public void DownloadAsync(Manifest[] manifests)
- {
- foreach (var m in manifests)
- {
- downloader.DownloadAsync(m);
- }
- }
- /// <summary>
- /// 下载完毕后执行的启动操作
- /// </summary>
- /// <param name="manifests"></param>
- public void Activate(Manifest[] manifests)
- {
- foreach (var m in manifests)
- {
- OnActivationInitializing(new ManifestEventArgs() { Manifest = m });
- Backup(m, downloader.TempPath);
- ActivationStartedEventArgs e = new ActivationStartedEventArgs() { Manifest = m };
- OnActivationStarted(e);
- if (e.Cancel)
- {
- Clear();
- break;
- }
- else
- {
- fileCopyer.CopyAsync(m, downloader.TempPath);
- }
- }
- }
- /// <summary>
- /// 备份操作
- /// </summary>
- /// <param name="manifest">文件清单</param>
- private void Backup(Manifest manifest, string sourcePath)
- {
- try
- {
- //string sourcePath = Path.GetFullPath(manifest.MyApplication.Location);
- string s_filename = string.Empty;
- string t_filename = string.Empty;
- if (!Directory.Exists(backupFilePath))
- {
- Directory.CreateDirectory(backupFilePath);
- }
- foreach (var file in manifest.ManifestFiles.Files)
- {
- t_filename = Path.Combine(backupFilePath, file.Source);
- s_filename = Path.Combine(sourcePath, file.Source);
- if (File.Exists(s_filename))
- {
- File.Copy(s_filename, t_filename, true);
- }
- }
- }
- catch (Exception ex)
- {
- LogHelper.log.Error("备份文件出现错误" + ex);
- }
- }
- /// <summary>
- /// 回滚文件下载内容
- /// </summary>
- /// <param name="manifest"></param>
- public void Rollback(Manifest manifest)
- {
- try
- {
- string filename = string.Empty;
- foreach (var file in manifest.ManifestFiles.Files)
- {
- filename = Path.Combine(backupFilePath, file.Source);
- File.Copy(filename, Path.Combine(Path.GetFullPath(manifest.MyApplication.Location), file.Source));
- }
- //Directory.Delete(backupFilePath, true);
- }
- catch (Exception ex)
- {
- LogHelper.log.Error("回滚文件出现错误" + ex);
- }
- }
- /// <summary>
- /// 清除临时文件
- /// </summary>
- private void Clear()
- {
- try
- {
- //Directory.Delete(backupFilePath, true);
- Directory.Delete(downloader.TempPath, true);
- }
- catch (Exception ex)
- {
- LogHelper.log.Error("清理临时文件出现错误" + ex);
- }
- }
- #endregion
- #region 事件处理
- private void fileCopyer_FileCopyError(object sender, FileCopyErrorEventArgs e)
- {
- OnActivationError(e);
- }
- private void fileCopyer_FileCopyProgressChanged(object sender, FileCopyProgressChangedEventArgs e)
- {
- if (ActivationProgressChanged != null)
- {
- ActivationProgressChanged(sender, e);
- }
- }
- private void fileCopyer_FileCopyCompleted(object sender, FileCopyCompletedEventArgs e)
- {
- Clear();
- try
- {
- //BackupSourceFiles(e.Manifest, updateCfgView.Version);
- updateCfgView.Version = e.Manifest.Version;
- }
- catch (Exception ex)
- {
- LogHelper.log.Info("备份原始程序出错误, 错误为." + ex);
- }
- if (ActivationCompleted != null)
- {
- ActivationCompletedEventArgs evt = new ActivationCompletedEventArgs(e.Error, e.Cancelled, e.UserState);
- evt.Manifest = e.Manifest;
- OnActivationCompleted(evt);
- }
- }
- /// <summary>
- /// 打包备份原始版本程序
- /// </summary>
- /// <param name="Manifest"></param>
- private void BackupSourceFiles(Manifest manifest, string sourceVersion)
- {
- string folderFullName = new DirectoryInfo("../").FullName;//当前应用程序路径的上级目录 "../" + rootPath;
- DirectoryInfo TheFolder = new DirectoryInfo(folderFullName);
- foreach (DirectoryInfo NextFolder in TheFolder.GetDirectories())
- {
- if (NextFolder.Name == manifest.MyApplication.Expand)
- {
- string sourceFiles = folderFullName + NextFolder.Name;
- string targetFiles = backupFilePath + "/" + NextFolder.Name + "_" + sourceVersion + ".zip";
- ZipUtility.ZipFiles(sourceFiles, targetFiles, null);
- }
- }
- }
- private void downloader_DownloadProgressChanged(object sender, DownloadProgressEventArgs e)
- {
- if (DownloadProgressChanged != null)
- {
- DownloadProgressChanged(sender, e);
- }
- }
- private void downloader_DownloadError(object sender, DownloadErrorEventArgs e)
- {
- if (DownloadError != null)
- {
- DownloadError(sender, e);
- }
- }
- private void downloader_DownloadCompleted(object sender, DownloadCompleteEventArgs e)
- {
- if (DownloadCompleted != null)
- {
- DownloadCompleted(sender, e);
- }
- }
- private void OnActivationInitializing(ManifestEventArgs e)
- {
- if (ActivationInitializing != null)
- {
- ActivationInitializing(this, e);
- }
- }
- private void OnActivationStarted(ActivationStartedEventArgs e)
- {
- if (ActivationStarted != null)
- {
- ActivationStarted(this, e);
- }
- }
- private void OnActivationCompleted(ActivationCompletedEventArgs e)
- {
- if (ActivationCompleted != null)
- {
- ActivationCompleted(this, e);
- }
- }
- private void OnActivationError(FileCopyErrorEventArgs e)
- {
- if (ActivationError != null)
- {
- ActivationError(this, e);
- }
- }
- private void OnActivationProgressChanged(FileCopyProgressChangedEventArgs e)
- {
- if (ActivationProgressChanged != null)
- {
- ActivationProgressChanged(this, e);
- }
- }
- #endregion
- }
- }
|