WPF客户端自动升级

实现原理:通过一个辅助程序(更新程序.exe)比较本地版本号和服务器的版本,若服务器版本新则通过更新程序.exe下载服务器上资源(我是把最新的文件压缩成zip放到服务器上)到本地进行替换。

 

服务器放置的升级文件结构如图

 

  • 此时要有两个程序,一个是自己的主程序,另一个是更新程序.exe,更新程序负责检查版本号和下载更新,将更新程序放到主程序的目录下。
  • 在主程序界面渲染显示前,调用更新程序.exe进行版本检查,如果有新版本则进行更新,没有的话主程序继续执行。
  • 此时本地和服务器应该有个相同的配置文件,用来存放一些必需的数据,我这里用的xml文件,读取本地xml文件和服务器xml文件,比较版本信息(指的是主程序version和更新程序version)进而判断需要升级主程序或者是升级程序亦或两者都升级。
  • 如果发现主程序有新版本,启用更新程序.exe从服务器上下载Debug.zip文件及xml文件,将zip压缩包放到临时文件夹下,利用第三方解压库CL.IO.Zip.dll进行解压,解压完成后将解压得到的文件夹及文件递归复制到主程序目录下,然后在更新程序中用Process.Start(主程序路径)启动主程序,主程序启动成功则关闭更新程序.exe程序(可以通过杀进程操作),此时软件已经升级成功。(这里我主程序只是做了版本比较,至于下载以及升级都交给更新程序.exe来做,当然也可以通过主程序进行下载,感觉有点分散了。。。)
  • 如果发现更新程序.exe有新版本,则直接在主程序中下载更新程序.exe进行替换即可。
  • 两者都有最新版本,先按照第4步升级更新程序.exe,在按照第3步升级主程序。

 

部分压缩包下载及解压代码: 

public class DownloadHelper
    {
        /// <summary>
        /// 获取版本信息
        /// </summary>
        /// <param name="url">版本信息文件的url</param>
        /// <returns></returns>
        public static Tuple<bool,UpdateInfo> GetConfigInfo(string url)
        {
            try
            {
                if (string.IsNullOrEmpty(url))
                {
                    return new Tuple<bool, UpdateInfo>(false, null);
                }
                WebClient client = new WebClient();
                Stream s = client.OpenRead(new Uri(url));
                UpdateInfo info = XmlHelper.Instance.ReadVersionConfig(s);
                s.Close();
                return new Tuple<bool,UpdateInfo>(true,info);
            }
            catch (Exception)
            {
                return new Tuple<bool, UpdateInfo>(false, null);
            }
        }
        /// <summary>
        /// 解压缩,拷贝,删除
        /// </summary>
        /// <param name="sourcePath">zip的路径</param>
        /// <param name="targetPath">目的路径</param>
        /// <returns></returns>
        public static bool UnZip(string sourcePath, string targetPath)
        {
            try
            {
                string zipFile = Path.Combine(sourcePath, "temp.zip");
                string extractPath = Path.Combine(targetPath, "temp");
                if (!Directory.Exists(extractPath))
                {
                    Directory.CreateDirectory(extractPath);
                }
                ZipFile.ExtractToDirectory(zipFile, extractPath);//将zip文件拷贝到临时文件夹
                if (Directory.Exists(Path.Combine(extractPath, "SeriesApp")))
                {
                    extractPath = Path.Combine(extractPath, "SeriesApp");
                }
                //将临时文件夹下的文件复制到原程序路径中
                CopyDirectory(extractPath, sourcePath);//注意,此时临时文件夹为源地址,sourcePath为目标地址
                File.Delete(zipFile);//删除zip文件
                Directory.Delete(Path.Combine(targetPath, "temp"), true);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// 解压缩,拷贝,删除 
        /// </summary>
        /// <param name="sourcePath">zip的路径</param>
        /// <param name="targetPath">目的路径</param>
        /// <param name="pBar">ProgressBar显示进度</param>
        /// <returns></returns>
        public static bool UnZip(string sourcePath, string targetPath,System.Windows.Controls.ProgressBar pBar)
        {
            try
            {
                ZipHandler handler = ZipHandler.GetInstance();
                string zipFile = Path.Combine(sourcePath, "temp.zip");
                string extractPath = Path.Combine(targetPath, "temp");
                handler.UnpackAll(zipFile, extractPath, (num) =>
                {
                    pBar.Dispatcher.Invoke(() =>
                    {
                        pBar.Value = num;//进度条显示
                    });
                });
              
                if (Directory.Exists(Path.Combine(extractPath, "SeriesApp")))
                {
                    extractPath = Path.Combine(extractPath, "SeriesApp");
                }
                //将临时文件夹下的文件复制到原程序路径中
                CopyDirectory(extractPath, sourcePath);//注意,此时临时文件夹为源地址,sourcePath为目标地址
                File.Delete(zipFile);//删除zip文件
                Directory.Delete(Path.Combine(targetPath, "temp"), true);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        /// <summary>
        /// 下载zip文件
        /// </summary>
        /// <param name="zipUrl">zip的url</param>
        /// <param name="targetDirPath">目标文件夹路径</param>
        /// <returns></returns>
        public static bool DownloadZip(string zipUrl,string targetDirPath)
        {
            string zipFile = Path.Combine(targetDirPath, "temp.zip");
            if (!Directory.Exists(targetDirPath))
            {
                return false;
            }
            try
            {
                WebClient client = new WebClient();
                client.DownloadFile(new Uri(zipUrl), zipFile);
                return true;
            }   
            catch (Exception)
            {
                return false;
            }
        }
        /// <summary>
        /// 下载xml配置
        /// </summary>
        /// <param name="url"></param>
        /// <param name="targetPath"></param>
        /// <returns></returns>
        public static bool DownLoadXMLConfig(string url, string targetPath)
        {
            try
            {
                var xmlPath = Path.Combine(targetPath, "VersionConfig.xml");
                WebClient client = new WebClient();
                client.DownloadFile(new Uri(url), xmlPath);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        /// <summary>
        /// 获取Zip的总大小
        /// </summary>
        /// <param name="zipUrl"></param>
        /// <returns></returns>
        public static double GetZipTotalSize(string zipUrl)
        {

            try
            {
                WebClient client = new WebClient();
                byte[] sr = client.DownloadData(new Uri(zipUrl));
                return sr.Length;
            }
            catch (Exception)
            {
                return 0;
            }
        }
        /// <summary>
        /// 递归copy文件
        /// </summary>
        /// <param name="sourcePath"></param>
        /// <param name="targetPath"></param>
        private static void CopyDirectory(string sourcePath, string targetPath)
        {
            try
            {
                if (!Directory.Exists(targetPath))
                {
                    Directory.CreateDirectory(targetPath);
                }
                string[] files = Directory.GetFiles(sourcePath);//Copy文件
                foreach (string file in files)
                {
                    try
                    {
                        string pFilePath = targetPath + "\\" + Path.GetFileName(file);
                        File.Copy(file, pFilePath, true);
                    }
                    catch (Exception)
                    {
                        continue;
                    }
                }

                string[] dirs = Directory.GetDirectories(sourcePath);//Copy目录
                foreach (string dir in dirs)
                {
                    CopyDirectory(dir, targetPath + "\\" + Path.GetFileName(dir));
                }
            }
            catch (Exception ex)
            {

            }
            }
        }

 完整项目地址

posted @ 2020-05-08 13:24  咸鱼戏花猫  阅读(4992)  评论(1编辑  收藏  举报