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;
} }
}

winform 更新下载压缩文件解压并覆盖的更多相关文章

  1. java压缩文件解压:调用WinRAR5命令强于自己写代码实现

    最近,手上维护着一个几年前的系统,技术是用的JSP+Strust2,系统提供了rar和zip两种压缩格式的解压功能,后台是用java实现的 1.解压rar格式,采用的是java-unrar-0.3.j ...

  2. SharpCompress的压缩文件解压和文件夹压缩

    1.前言 最近做一个功能需要用到对压缩文件的解压,就找到了这个SharpCompress不错,还能解压rar的文件.但是网上的资料和我拿到的SharpCompress.dll的方法有些出入,所以我就自 ...

  3. 《OD学hadoop》在LINUX下如何将tar压缩文件解压到指定的目录下

    linux下tar命令解压到指定的目录 :#tar zxvf /bbs.tar.zip -C /zzz/bbs //把根目录下的bbs.tar.zip解压到/zzz/bbs下,前提要保证存在/zzz/ ...

  4. linux压缩文件——解压方法

    linux下 tar解压 gz解压 bz2等各种解压文件使用方法 .tar 解包:tar xvf FileName.tar 打包:tar cvf FileName.tar DirName (注:tar ...

  5. ubuntu 各压缩文件解压命令大全

    .tar 解包:tar xvf xxx.tar 打包:tar cvf xxx.tar DirName (注:tar是打包,不是压缩!) .gz 解压1:gunzip FileName.gz 解压2:g ...

  6. Linux 学习笔记 5 文件的下载、压缩、解压、初步认识yum

    写在前面 上节我们通过简单的几组命令,已经完全的实现了文件的移动.删除.更名.以及复制,我们最常用的基本玩法,本节将带着大家学习压缩.解压的相关步骤. Linux 学习笔记 4 创建.复制.移动.文件 ...

  7. 16 Linux系统的文件压缩、解压与归档

    这一节的内容,我们详细介绍下Linux的文件压缩.解压缩与文件归档的内容,也就是tar.gzip.bzip2.xz等命令的内容: 压缩(compress)与解压缩(uncompress) Linux系 ...

  8. Linux 下 zip 文件解压乱码解决方案,ubuntu16.10亲测可用

    文章来源: https://www.zhihu.com/question/20523036 今天邮件中收到了一个压缩文件,解压后却是乱码,从网上也找了几个方法,目前这个方法还是比较可靠的,如下所示: ...

  9. Linux系列之压缩与解压

    1.压缩技术 1.常用命令实例 1.zip格式的压缩与解压缩 zip是压缩指令,unzip是解压指令.zip指令既可以压缩文件,也可以压缩目录.压缩会自动保留源文件,解压会自动保留压缩文件. zip  ...

  10. Linux | 压缩与解压详解

    tar tar 命令用于对文件进行打包压缩或解压,格式: tar [选项][文件] tar命令的参数及其作用 参数 作用 -c 创建压缩文件 -x 解开压缩文件 -t 查看压缩包内有哪些文件 -z 用 ...

随机推荐

  1. 04-Sed操作参数

    1 Sed操作参数 1.1 s--替换 s表示替换(substitute)文件内的字符串. [address1],[address2]s/pattern/replacement/[flag] # s/ ...

  2. CF1237H Balanced Reversals

    H - Balanced Reversals 首先可以将相邻的两个点分到一个组中 特判无解的情况:00的数量不相等或11的数量不相等 若10的数量相等(此时01的数量也相等,因为知道10的数量后01的 ...

  3. NodeJS 实战系列:如何设计 try catch

    本文将通过一个 NodeJS 程序里无效的错误捕获示例,来讲解错误捕获里常见的陷阱.错误捕获不是凭感觉添加 try catch 语句,它的首要目的是提供有效的错误排查信息,只有精心设计的错误捕获才有可 ...

  4. 【分析笔记】全志方案通过命令行操作 GPIO 口(带源码分析)

    前言说明 在项目开发初期,很经常会需要临时操作某个GPIO来验证某些功能,可以通过编写一个简单的驱动程序来操作,但更方便的是可以通过命令行直接操作 GPIO ,这样不需要经过编写代码.编译驱动.推入文 ...

  5. 开源分布式支持超大规模数据分析型数据仓库Apache Kylin实践-上

    @ 目录 概述 定义 特性 术语 技术概念 架构和组件 生态圈 部署 Docker部署 基于hadoop环境安装 前置条件 安装 使用步骤 官方样例Cube说明 示例演示 准备演示数据 创建项目 选择 ...

  6. 数据库系列:MySQL慢查询分析和性能优化

    1 背景 我们的业务服务随着功能规模扩大,用户量扩增,流量的不断的增长,经常会遇到一个问题,就是数据存储服务响应变慢. 导致数据库服务变慢的诱因很多,而RD最重要的工作之一就是找到问题并解决问题. 下 ...

  7. 【已解决】rust 报错 no_std can't find crate for `test` || vscode 工作区多文件夹

    遇到下面的问题,请修改vscode的设置文件 如下 { "rust-analyzer.checkOnSave.allTargets": false, "rust-anal ...

  8. rt-thread模糊到清晰系列: timer.c

    #include <rtthread.h> #include <rthw.h> /* hard timer list */ static rt_list_t rt_timer_ ...

  9. 三分钟实战手写Spring Boot Starter

    1 背景 在平时的开发中,开发的同学会把一些通用的方法,写成一个工具类,例如日期转换的,JSON转换的等等,方便业务后续调用,使代码更容易维护. 如果一些更常用的方法,例如鉴权的,加解密的等等,几乎每 ...

  10. clicknium-划时代的自动化

    说起UI自动化,selenium一直是不可替代的存在,它的安装量.百度数量已经奠定了它在自动化.爬虫等相关领域的霸主低位,可以说,只要涉及到UI自动化,选selenium就对了. 细数过往,相关的UI ...