Wpf 客户端比较版本号差异自动升级
最编程
2024-10-19 06:56:25
...
实现原理:通过一个辅助程序(更新程序.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) { } } }
上一篇: [机器学习基础] nn.
下一篇: Python 编程:常用模块分类组织
推荐阅读
-
Wpf 客户端比较版本号差异自动升级
-
ssh工作流程及原理-SSH(Secure Shell Protocol,安全的壳程序协议),它可以通过数据包加密技术将等待传输的数据包加密后再传输到网络上。ssh协议本身提供两个服务器功能:一个是类似telnet的远程连接使用shell的服务器;另一个就是类似ftp服务的sftp-server,提供更安全的ftp服务。 连接加密技术简介 目前常见的网络数据包加密技术通常是通过“非对称密钥系统”来处理的。主要通过两把不一样的公钥与私钥来进行加密与解密的过程。 公钥(public key):提供给远程主机进行数据加密的行为,所有人都可获得你的公钥来将数据加密。 私钥(private key):远程主机使用你的公钥加密的数据,在本地端就能够使用私钥来进行解密。私钥只有自己拥有。 SSH工作过程:在整个通讯过程中,为实现SSH的安全连接,服务端与客户端要经历如下五个阶段: 版本号协商阶段 SSH目前包括SSH1和SSH2两个版本,双方通过版本协商确定使用的版本 密钥和算法协商阶段 SSH支持多种加密算法,双方根据本端和对端支持的算法,协商出最终使用的算法 认证阶段 SSH客户端向服务器端发起认证请求,服务器端对客户端进行认证 会话请求阶段 认证通过后,客户端向服务器端发送会话请求 交互会话阶段 会话请求通过后,服务器端和客户端进行信息的交互 一、版本协商阶段 服务器端打开端口22,等待客户端连接; 客户端向服务器端发起TCP初始连接请求,TCP连接建立后,服务器向客户端发送第一个报文,包括版本标志字符串,格式为“SSH-<主协议版本号>.<次协议版本号>.<软件版本号>”,协议版本号由主版本号和次版本号组成,软件版本号主要是为调试使用。 客户端收到报文后,解析该数据包,如果服务器的协议版本号比自己的低,且客户端能支持服务器端的低版本,就使用服务器端的低版本协议号,否则使用自己的协议版本号。 客户端回应服务器一个报文,包含了客户端决定使用的协议版本号。服务器比较客户端发来的版本号,决定是否能同客户端一起工作。如果协商成功,则进入密钥和算法协商阶段,否则服务器断开TCP连接。 说明:上述报文都是采用明文方式传输。 二、密钥和算法协商阶段 服务器端和客户端分别发送算法协商报文给对端,报文中包含自己支持的公钥算法列表、加密算法列表、MAC(Message Authentication Code,消息验证码)算法列表、压缩算法列表等等。 服务器端和客户端根据对端和本端支持的算法列表得出最终使用的算法。 服务器端和客户端利用DH交换(Diffie-Hellman Exchange)算法、主机密钥对等参数,生成会话密钥和会话ID。 由此,服务器端和客户端就取得了相同的会话密钥和会话ID。对于后续传输的数据,两端都会使用会话密钥进行加密和解密,保证了数据传送的安全。在认证阶段,两端会使用会话用于认证过程。 会话密钥的生成: 客户端需要使用适当的客户端程序来请求连接服务器,服务器将服务器的公钥发送给客户端。(服务器的公钥产生过程:服务器每次启动sshd服务时,该服务会主动去找/etc/ssh/ssh_host*文件,若系统刚装完,由于没有这些公钥文件,因此sshd会主动去计算出这些需要的公钥文件,同时也会计算出服务器自己所需要的私钥文件。) 服务器生成会话ID,并将会话ID发给客户端。 若客户端第一次连接到此服务器,则会将服务器的公钥数据记录到客户端的用户主目录内的~/.ssh/known_hosts。若是已经记录过该服务器的公钥数据,则客户端会去比对此次接收到的与之前的记录是否有差异。客户端生成会话密钥,并用服务器的公钥加密后,发送给服务器。 ****服务器用自己的私钥将收到的数据解密,获得会话密钥。 服务器和客户端都知道了会话密钥,以后的传输都将被会话密钥加密。 三、认证阶段 SSH提供两种认证方法: 基于口令的认证(password认证):客户端向服务器发出password认证请求,将用户名和密码加密后发送给服务器,服务器将该信息解密后得到用户名和密码的明文,与设备上保存的用户名和密码进行比较,并返回认证成功或失败消息。 基于密钥的认证(publickey认证):客户端产生一对公共密钥,将公钥保存到将要登录的服务器上的那个账号的家目录的.ssh/authorized_keys文件中。认证阶段:客户端首先将公钥传给服务器端。服务器端收到公钥后会与本地该账号家目录下的authorized_keys中的公钥进行对比,如果不相同,则认证失败;否则服务端生成一段随机字符串,并先后用客户端公钥和会话密钥对其加密,发送给客户端。客户端收到后将解密后的随机字符串用会话密钥发送给服务器。如果发回的字符串与服务器端之前生成的一样,则认证通过,否则,认证失败。 注:服务器端对客户端进行认证,如果认证失败,则向客户端发送认证失败消息,其中包含可以再次认证的方法列表。客户端从认证方法列表中选取一种认证方法再次进行认证,该过程反复进行。直到认证成功或者认证次数达到上限,服务器关闭连接为止。实例