解压zip文件自动覆盖实现主程序自动更新功能

本文介绍了一个使用.NET Zip库进行应用程序升级的Windows Forms应用 UpgraderA,它从XML配置中读取版本号,解压升级文件并替换旧版本,确保无缝更新过程。遇到问题时,会提示管理员处理错误。
摘要由CSDN通过智能技术生成

必须下载nuget上的donetzip的dll引用文件。

using Ionic.Zip;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using System.Xml;

namespace UpgraderA
{
    public partial class UpgraderA : Form
    {
        private string currentPath = Application.StartupPath;

        public UpgraderA()
        {
            InitializeComponent();
            ShowInTaskbar = false;
            WindowState = FormWindowState.Minimized;
        }

        private void Upgrade()
        {
            DirectoryInfo info = new DirectoryInfo(Application.StartupPath);
            string path = currentPath + "\\upgradefiles\\";
            string version = string.Empty;
            string sourcePath = string.Empty;
            XmlTextReader textReader = new XmlTextReader(info.Parent.FullName + "\\AutoUpgrader.xml");
            while (textReader.Read())
            {
                if (textReader.Name == "versionNo")
                {
                    version = textReader.GetAttribute("Number");
                    textReader.Close();
                    break;
                }
            }
            if (version != null)
            {
                sourcePath = currentPath + "\\upgradefiles\\Debug(" + version + ").zip";
            }
            else
            {
                return;
            }
            try
            {
                using (ZipFile zip = new ZipFile(sourcePath))
                {
                    zip.ExtractAll(path, ExtractExistingFileAction.OverwriteSilently);
                }
                List<FileInfo> listinfos = FindAllFiles(currentPath + "\\upgradefiles\\Debug(" + version + ")");
                foreach (var file in listinfos)
                {
                //包含文件夹的文件复制不过去,必须先建立相应的路径文件夹
                    string[] strArr = file.FullName.Split(new[] { "Debug(" + version + ")" }, StringSplitOptions.None);
                    if (!File.Exists(info.Parent.FullName + strArr[1]))
                    {
                        string str = (info.Parent.FullName + strArr[1]).Replace(file.Name,"");
                        if (!Directory.Exists(str))
                        {
                            Directory.CreateDirectory(str);
                        }
                    }
                    File.Copy(file.FullName, info.Parent.FullName + strArr[1], true);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("升级出错,请联系管理员或开发人员!" + ex.Message);
            }
            finally
            {
                string filename = currentPath + "\\..\\LawRecorderWs.exe";
                if (File.Exists(sourcePath))
                {
                    File.Delete(sourcePath);
                }
                if (Directory.Exists(currentPath + "\\upgradefiles\\Debug(" + version + ")"))
                {
                    Directory.Delete(currentPath + "\\upgradefiles\\Debug(" + version + ")", true);
                }
                FileInfo newFile = new FileInfo(filename);
                if (newFile.Exists)
                {
                    Process.Start(filename);
                    Process.GetCurrentProcess().Kill();
                }
            }
        }
        /// <summary>
        /// 使用递归方法查询路径下的所有文件信息
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        private static List<FileInfo> FindAllFiles(string filePath)
        {
            List<FileInfo> infoList = new List<FileInfo>();
            if (Directory.Exists(filePath))
            {
                FileInfo[] allFile = new DirectoryInfo(filePath).GetFiles();
                foreach (FileInfo file in allFile)
                {
                    infoList.Add(file);
                }
                DirectoryInfo[] allDir = new DirectoryInfo(filePath).GetDirectories();
                foreach (DirectoryInfo dir in allDir)
                {
                    infoList.AddRange(FindAllFiles(dir.FullName));
                }
            }
            return infoList;
        }


        private void UpgraderA_Load(object sender, EventArgs e)
        {
            try
            {
                if (currentPath.Substring(currentPath.Length - 2) == "\\")
                {
                    currentPath = currentPath.Substring(0, currentPath.Length - 2);
                }
                //用子线程工作
                Thread t = new Thread(new ThreadStart(Upgrade))
                {
                    IsBackground = true//设为后台线程
                };
                t.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值