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;
} }
}
winform 更新下载压缩文件解压并覆盖的更多相关文章
- java压缩文件解压:调用WinRAR5命令强于自己写代码实现
最近,手上维护着一个几年前的系统,技术是用的JSP+Strust2,系统提供了rar和zip两种压缩格式的解压功能,后台是用java实现的 1.解压rar格式,采用的是java-unrar-0.3.j ...
- SharpCompress的压缩文件解压和文件夹压缩
1.前言 最近做一个功能需要用到对压缩文件的解压,就找到了这个SharpCompress不错,还能解压rar的文件.但是网上的资料和我拿到的SharpCompress.dll的方法有些出入,所以我就自 ...
- 《OD学hadoop》在LINUX下如何将tar压缩文件解压到指定的目录下
linux下tar命令解压到指定的目录 :#tar zxvf /bbs.tar.zip -C /zzz/bbs //把根目录下的bbs.tar.zip解压到/zzz/bbs下,前提要保证存在/zzz/ ...
- linux压缩文件——解压方法
linux下 tar解压 gz解压 bz2等各种解压文件使用方法 .tar 解包:tar xvf FileName.tar 打包:tar cvf FileName.tar DirName (注:tar ...
- ubuntu 各压缩文件解压命令大全
.tar 解包:tar xvf xxx.tar 打包:tar cvf xxx.tar DirName (注:tar是打包,不是压缩!) .gz 解压1:gunzip FileName.gz 解压2:g ...
- Linux 学习笔记 5 文件的下载、压缩、解压、初步认识yum
写在前面 上节我们通过简单的几组命令,已经完全的实现了文件的移动.删除.更名.以及复制,我们最常用的基本玩法,本节将带着大家学习压缩.解压的相关步骤. Linux 学习笔记 4 创建.复制.移动.文件 ...
- 16 Linux系统的文件压缩、解压与归档
这一节的内容,我们详细介绍下Linux的文件压缩.解压缩与文件归档的内容,也就是tar.gzip.bzip2.xz等命令的内容: 压缩(compress)与解压缩(uncompress) Linux系 ...
- Linux 下 zip 文件解压乱码解决方案,ubuntu16.10亲测可用
文章来源: https://www.zhihu.com/question/20523036 今天邮件中收到了一个压缩文件,解压后却是乱码,从网上也找了几个方法,目前这个方法还是比较可靠的,如下所示: ...
- Linux系列之压缩与解压
1.压缩技术 1.常用命令实例 1.zip格式的压缩与解压缩 zip是压缩指令,unzip是解压指令.zip指令既可以压缩文件,也可以压缩目录.压缩会自动保留源文件,解压会自动保留压缩文件. zip ...
- Linux | 压缩与解压详解
tar tar 命令用于对文件进行打包压缩或解压,格式: tar [选项][文件] tar命令的参数及其作用 参数 作用 -c 创建压缩文件 -x 解开压缩文件 -t 查看压缩包内有哪些文件 -z 用 ...
随机推荐
- Linux c 获取U盘挂载路径
思路: 1.执行df -h 找到 带mnt的行.将结果存入一个文件中. system("df -h |grep mnt >./extendevinfo.txt"); 也可以直 ...
- dfs 返回值用bool相对void会快一点
力扣 剑指 Offer 12. 矩阵中的路径 超时代码 dfs返回值是void,用类内的全局变量flag表示找到或没找到. class Solution { public: bool flag; in ...
- vs2019 内核驱动编译失败:网络共享盘问题
怀念2008 我以为这个年代了,中文路径问题应该解决了,记得vs 2008写c的时候曾因中文路径的问题导致编译不通过,从2015版本开始发现有时候项目名称和路径里包含中文也可以编译通过了,习惯之后后面 ...
- Vue3的script setup语法糖这么好用的吗????
最近发现这个vue3居然还可以这样写 原始写法 <template> <h1>Tangdoudou</h1> <h1>{{ num }}</h1& ...
- djiango路由匹配、djiango路由层、反向解析、有名无名反向解析
djiango路由匹配.djiango路由层.反向解析.有名无名反向解析 一.作业讲解(数据的增删改查) urls代码 from django.contrib import admin from dj ...
- 如何通过Java应用程序添加或删除 PDF 中的附件
当我们在制作PDF文件或者PPT演示文稿的时候,为了让自己的文件更全面详细,就会在文件中添加附件.并且将相关文档附加到 PDF 可以方便文档的集中管理和传输.那么如何添加或删除 PDF 中的附件呢?别 ...
- 注解_概念-注解_JDK内置注解
注解_概念 注解: 概念:说明程序的.给计算机看的 注释:用文字描述程序的.给程序员看的 定义︰注解(Annotation),也叫元数据.一种代码级别的说明.它是J0K1.5及以后版本引久的一个特性, ...
- 【新晋开源项目】内网穿透神器[中微子代理] 加入 Dromara 开源社区
1.关于作者 dromara开源组织成员,dromara/neutrino-proxy项目作者 名称:傲世孤尘.雨韵诗泽 名言: 扎根土壤,心向太阳.积蓄能量,绽放微光. 拘浊酒邀明月,借赤日暖苍穹. ...
- django框架之drf:04、序列化器常用字段及参数,序列化器高级用法之source、定制字段数据的两种方法、多表关联反序列化的保存、ModelSerializer的使用
Django框架之drf 目录 Django框架之drf 一.序列化器常用字段及参数 1.常用字段 2.常用字段参数 3.字段参数针对性分类 二.序列化器高级用法之source 1.定制字段名 三.定 ...
- 1月9日内容总结——linux相关知识简介、虚拟化软件vmware、远程链接工具xshell
目录 一.linux常见岗位 二.计算机的种类与服务器 三.服务器品牌 四.服务器内部组成 五.服务器磁盘阵列 六.linux简介 1.什么是linux 2.linux发展史 3.Linux系统的特点 ...