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

最新文章

  1. java接口的应用举例
  2. 浏览器桌面通知Notification探究
  3. Shiro 整合SpringMVC 并且实现权限管理
  4. JavaScript--DOM修改元素的属性
  5. c++ 中 delete p与 delete []p的区别
  6. D3中path各指令的含义
  7. jdk 中Runtime之单例模式 学习
  8. jsonp 使用示例
  9. csu 10月 月赛 I 题 The Contest
  10. Could not load the Tomcat server configuration at /Servers/Tomcat v7.0 Server at localhost-config.
  11. 自制DTU
  12. 72_leetcode_Construct Binary Tree from Preorder and Inorder Traversal
  13. List实现
  14. OpenStack_I版 5.Nova部署
  15. ROS_Kinetic_23 ROS流行版本和相关书籍汇总
  16. 二十四、Hadoop学记笔记————Spark的架构
  17. Dev GridControl数据修改后实时更新数据源(转)
  18. python项目练习
  19. CFX构建webservice实例,与Spring整合.
  20. 使用Oozie中workflow的定时任务重跑hive数仓表的历史分期调度

热门文章

  1. angular Ionic CLI项目开始
  2. 为啥要对jvm做优化?
  3. 力扣---1306. 跳跃游戏 III
  4. Vue17 手机表单数据
  5. 为K8S集群准备Ceph存储
  6. 模拟实现strlen的三种方法
  7. TIM + DMA + ADC
  8. spark数据清洗
  9. 【JavaScript】JS引擎中执行上下文如何顺序执行代码
  10. 题解 CF17201 A~D2