健康一贴灵,专注医药行业管理信息化

C#客户端程序自动更新

困绕多年的自动升级问题,完美解决,主要通过此文件结合批处理方式

思路:WINFORM程序启动时,检查本机的local.xml和服务器上的server.xml文件的版本号,如果有新的,就执行下载服务器上的ZIP压缩文件到本机的temp目录;

下载完成后,退出WINFORM程序,启动本地的update.bat批处理;

1、批处理文件在程序目录下的temp目录中,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%
del drp.exe 
cd ..
%TG%

exit

 

AutoUpdate.cs

using drp.Common;
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.Xml;

namespace drp
{
    //自动更新程序
    //需要架设WEB服务器,目录下有update.bat和server.xml两个文件;本地程序中要有local.xml 文件

    public class AutoUpdate
    {
        public static string localVersion = "";
        public static string serverXml = "";
        public static string releaseVersion = "";
        public static string releaseUrl = "";

        /// <summary>
        /// 获取升级URL地址和版本号
        /// </summary>
        /// <returns>0 失败 1 成功</returns>
        public static string getUpdateXml()
        {
            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;//服务器端的软件版本
                releaseUrl = node2.SelectSingleNode("ReleaseUrl").InnerText; //要下载的ZIP文件}
                                                                             //如果版本相同,或取不到服务器版本号,则直接登录
                if (localVersion == releaseVersion || releaseVersion == "")
                {
                    return releaseUrl;
                }
                else
                {
                    return "0";
                }
            }
            catch
            {
                return "0";
            }
            return "0";

        }

        /// <summary>
        /// 下载压缩文件并更新本地XML文件的版本号
        /// </summary>
        /// <returns>0 失败 1 成功</returns>
        public static string downLoadZip()
        {
            try
            {
                using (WebClient web = new WebClient())
                {
                    //本地下载目录,位于debug下的temp目录,本地需要有这个目录
                    string tmpPath = AppDomain.CurrentDomain.BaseDirectory + "temp";
                    //要解压的文件名及地址
                    string unZipFile = Path.Combine(tmpPath, "drp.zip");

                    //从升级服务中下载ZIP文件(releaseUrrl)到本地指定目录及文件名
                    web.DownloadFile(releaseUrl, 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,则不显示窗口
                    };
                    //将本地Local.xml文件的的版本改为和服务器版本一致
                    CommFunc.SetXmlValue("Local.xml", "//LocalUpdate", "LocalVersion", releaseVersion);
                    // 创建并启动进程,执行批处理文件
                    using (Process process = Process.Start(startInfo))
                    {
                        process.WaitForExit(); // 等待进程退出
                    }
                    return "1";

                }
            }
            catch
            {
                return "0";
            }
            return "1";
        }

        /*
         * 附XML 文件样例:
         * */

    }
}

 附:server.xml 文件放web服务器上

<?xml version="1.0" encoding="utf-8" ?>
<ServerUpdate>
  <item>
    <ApplicationStart>drp.exe</ApplicationStart>
    <AppName>Print</AppName>
    <MinVersion>1.0.0.0</MinVersion>
    <ReleaseDate>2017/9/28 9:25:24</ReleaseDate>
    <ReleaseUrl>http://39.107.225.109/download/drp.zip</ReleaseUrl>
    <ReleaseVersion>2.0.5</ReleaseVersion>
    <VersionDesc>1</VersionDesc>
    <UpdateMode>Increment</UpdateMode>
  </item>
</ServerUpdate>

 

local.xml

<LocalUpdate>
  <LocalVersion>2.0.3.20</LocalVersion>
  <LocalIgnoreVersion>1.1.0.0</LocalIgnoreVersion>
  <LastUdpate>2017/9/28 9:25:24</LastUdpate>
  <ServerUpdateUrl>http://10.10.10.108/download/Server.xml</ServerUpdateUrl>
</LocalUpdate>

 

posted @ 2025-07-04 15:13  一贴灵  阅读(33)  评论(0)    收藏  举报
学以致用,效率第一