winform 更新下载压缩文件解压并覆盖

 

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using ICSharpCode.SharpZipLib.Zip;
using UpdateDevForm.Response;

namespace UpdateDevForm
{
    public partial class UpdateForm : Form
    {
        public readonly string UpdateFiles = Path.Combine(Application.StartupPath, "update");//解压到的本机地址
        public string CheckUpdateURL;  //更新包所在网络地址(服务器端的路径)
        public string resourcetype; //安装方式 :重新安装 0,文件覆盖 1
        public string resourcename;  //文件名称带后缀名

        public UpdateForm()
        {
            InitializeComponent();
        }

        private void UpdateForm_Load(object sender, EventArgs e)
        {

            if (!UpdateHelper.IsConnectInternet())
            {
                MessageBox.Show("网络异常,请检查网络连接", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.Exit();
            }

            ResponseResult<UpdateInfo> updateInfo = UpdateHelper.GetUpdateInfo("Windows_CSOnline");
            if (updateInfo == null)
            {
#if DEBUG
                UpdateHelper.StartKillProcess(System.IO.Path.Combine(@"E:\xx\xx\xx\\bin\Debug", @"xxx.exe"), "UpdateDevForm");
#else               
                UpdateHelper.StartKillProcess(System.IO.Path.Combine(Application.StartupPath, @"xxx.exe"), "UpdateDevForm");
#endif
            }
            else
            {
                CheckForIllegalCrossThreadCalls = false;
                Thread t = new Thread(() =>  
                {
                    if (!System.IO.Directory.Exists(UpdateFiles))
                    {
                        System.IO.Directory.CreateDirectory(UpdateFiles);
                    }
                    CheckUpdateURL = updateInfo.content.resourceUri.ToString();
                    resourcetype = updateInfo.content.resourceType;
                    resourcename = updateInfo.content.resourceName;

                    using (WebClient webClient = new WebClient())
                    {
                        try  
                        {    //通过webclient下载服务器端的文件
                            webClient.DownloadFile(new Uri(CheckUpdateURL), UpdateFiles + "\\" + resourcename);
                        }
                        catch (WebException ex)
                        {
                            MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            Application.Exit();
                        }
                    }

                    if (int.Parse(resourcetype) == 1) //文件覆盖
                    {
                        bool ConverFileResult = ConverFile(UpdateFiles, resourcename);

                        if (!ConverFileResult)
                        {
                            MessageBox.Show("更新失败", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            Application.Exit();
                        }
                    }
                    else if (int.Parse(resourcetype) == 0)  //重新安装
                    {
                        bool ReinstallResult = Reinstall(UpdateFiles + "\\" + resourcename);
                        if (!ReinstallResult)
                        {
                            MessageBox.Show("更新失败", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            Application.Exit();
                        }
                    }

                    UpdateBLL updateBLL = new UpdateBLL();
                    int count = updateBLL.Update("Versions", updateInfo.content.version);
                    if (count > 0)
                    {

                    }

                    MessageBox.Show("更新成功!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    UpdateHelper.StartKillProcess("xxx.exe", "UpdateDevForm");
                });
                t.IsBackground = true;
                t.Start();



            }

        }
        /// <summary>
        /// 重新安装
        /// </summary>
        /// <param name="MsiFilesUrl"></param>
        /// <returns></returns>
        private bool Reinstall(string MsiFilesUrl)
        {
            this.Hide();

            Process UninstallProcess = new Process();  //卸载进程
            UninstallProcess.StartInfo.FileName = "msiexec";
            UninstallProcess.StartInfo.Arguments = "/x {2ED75D70-FC7F-469C-9F71-24CB3A4E923C}";
            UninstallProcess.Start();
            UninstallProcess.WaitForExit();          
            if (UninstallProcess.ExitCode == 0)
            {

                Process installProcess = new Process();  //安装进程
                MessageBox.Show(MsiFilesUrl);
                installProcess.StartInfo.FileName = MsiFilesUrl;
                installProcess.StartInfo.Arguments = "";
                installProcess.Start();
                installProcess.WaitForExit();
                if (installProcess.ExitCode == 0)
                {

                    installProcess.Close();
                    return true;
                }
                else
                {
                    installProcess.Close();
                    return false;
                }

            }
            else
            {
                UninstallProcess.Close();
                return false;
            }


        }

        /// <summary>
        /// 文件覆盖
        /// </summary>
        /// <param name="zip_path">zip文件所在路径</param>
        /// <param name="resourcename">zip更新包文件名带后缀</param>
        /// <returns></returns>
        private bool ConverFile(string zip_path, string resourcename)
        {
            string filename = zip_path + "\\" + resourcename;
            if (!File.Exists(filename))
            {
                MessageBox.Show("下载更新包文件失败!");
                Application.Exit();
            }

            FastZip zip = new FastZip();
            zip.ExtractZip(filename, Application.StartupPath, "");

            #region  .NET Framework下此方法不能覆盖文件  .NET Core下此方法可以覆盖使用
            // 引入 System.IO.Compression;           
            // ZipFile.ExtractToDirectory(filename, Application.StartupPath);
            #endregion

            if (File.Exists(filename))
            {
                File.Delete(filename);//覆盖完成之后删除zip文件
                return true;
            }

            return false;
        }




    }
}

 

posted @ 2019-09-04 17:28  这个问题解决不了  阅读(212)  评论(0编辑  收藏  举报