winform程序结合批处理升级方法
以前使用网上MAutoUpdate更新程序,更新WINFORM桌面程序用的非常顺利,换了WIN11以后,各种BUG,后来试用clickonce 来升级,基本可以用,唯一的问题是处理不好xml 文件的发布,
受同事自动备份批处理的启发,结合批处理文件,完全实现自动更新;
程序目录结构 :debug 下建”temp"目录,放置update.bat批处理文件;
程序中使用了ICSharpCode.SharpZipLib 类库,来解压ZIP文件。
附代码:
一、program.cs 程序
using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Xml; using drp.Common; using drp.DB; using ICSharpCode.SharpZipLib; using ICSharpCode.SharpZipLib.Zip; namespace drp { static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] //判断是否登录成功 static void Main() { //key:9445bf337ad8bda1614aeaa955be2a6c Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); string localVersion = ""; string filePath = ""; string serverXml = ""; string releaseVersion = ""; string updateFile = ""; try { //读取本地Local.xml文件中的软件版本,服务器端的软件版本,如果本地小于服务器端,则更新; XmlDocument xmldoc = new XmlDocument(); xmldoc.Load("Local.xml"); XmlNode node = xmldoc.SelectSingleNode("//LocalUpdate"); localVersion = node.SelectSingleNode("LocalVersion").InnerText; serverXml = node.SelectSingleNode("ServerUpdateUrl").InnerText; //读取服务器端的Server.xml文件 XmlReader xml = XmlReader.Create(serverXml); XmlDocument xdoc = new XmlDocument(); xdoc.Load(serverXml); XmlNode node2 = xdoc.SelectSingleNode("//ServerUpdate/item"); releaseVersion = node2.SelectSingleNode("ReleaseVersion").InnerText;//服务器端的软件版本 updateFile = node2.SelectSingleNode("ReleaseUrl").InnerText; //要下载的ZIP文件 //如果版本相同,或取不到服务器版本号,则直接登录 if (localVersion == releaseVersion || releaseVersion=="") { FrmLogin frmLogin = new FrmLogin(); if (frmLogin.ShowDialog() == DialogResult.OK) { Application.Run(new FrmMain()); } } else { using (WebClient web = new WebClient()) { //本地下载目录 string tmpPath = AppDomain.CurrentDomain.BaseDirectory + "temp"; //资源目录 string urlAdd = "http://127.0.0.1:8080/flow/drp.zip"; string unZipFile = Path.Combine(tmpPath, "drp.zip"); web.DownloadFile(urlAdd, unZipFile); ZipHelper.UnZip(unZipFile, tmpPath); // 批处理文件的路径 string batchFilePath = AppDomain.CurrentDomain.BaseDirectory+"temp" ; batchFilePath = Path.Combine(batchFilePath, "update.bat"); // 创建ProcessStartInfo对象 ProcessStartInfo startInfo = new ProcessStartInfo { FileName = batchFilePath, UseShellExecute = true, // 使用Shell执行,这样可以看到批处理文件的窗口 CreateNoWindow = false // 如果设置为true,则不显示窗口 }; CommFunc.SetXmlValue("Local.xml", "//LocalUpdate", "LocalVersion", releaseVersion); // 创建并启动进程,执行批处理文件 using (Process process = Process.Start(startInfo)) { process.WaitForExit(); // 等待进程退出 } //将本地版本号更改为服务器端的版本号 } } } catch (Exception ex) { // 处理异常 MessageBox.Show(" 读取版本信息出错:\r\n" + ex.Message); return; } } } }
二 、update.bat批处理文件
 
@echo off echo 正在强制关闭 drp.exe 进程... taskkill /F /IM drp.exe >nul 2>&1 ::设置程序运行的目录;%~dp0为当前目录; %~dp0..\ 为当前目录的上一级目录 SET SUBPATH = %~dp0 SET TARGETPATH=%~dp0..\ SET TG= %TARGETPATH%drp.exe if %errorlevel% equ 0 ( echo 成功终止 drp.exe 进程! ) else ( echo 未能终止 drp.exe 进程,可能原因: echo 1. 进程不存在 echo 2. 权限不足(请以管理员身份运行此脚本) echo 3. 进程已被保护 ) echo 当前目录为 %SUBPATH% cd %SUBPATH % copy %SUBPATH%drp.exe %TG% timeout /t 2 /nobreak >nul ECHO %TARGETPATH% ECHO %TG% cd .. %TG% exit
三、Local.xml
<LocalUpdate> <LocalVersion>2.0.3.16</LocalVersion> <LocalIgnoreVersion>1.1.0.0</LocalIgnoreVersion> <LastUdpate>2017/9/28 9:25:24</LastUdpate> <ServerUpdateUrl>http://127.0.0.1:8080/flow/Server.xml</ServerUpdateUrl> </LocalUpdate>
四、Server.xml
<?xml version="1.0" encoding="utf-8" ?> <ServerUpdate> <item> <ReleaseUrl>http://127.0.0.1:8080/flow/drp.zip</ReleaseUrl> <ReleaseVersion>2.0.3.16</ReleaseVersion> </item> </ServerUpdate>
    活到老,学到老。
 
                     
                    
                 
                    
                
 
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号