You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
241 lines
8.3 KiB
241 lines
8.3 KiB
using Ionic.Zip;
|
|
using System;
|
|
using System.ComponentModel;
|
|
using System.Configuration;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Net;
|
|
using System.Text;
|
|
using System.Windows.Forms;
|
|
|
|
namespace Update
|
|
{
|
|
public partial class MainFrom : Form
|
|
{
|
|
readonly string programName = ConfigurationManager.AppSettings["ProgramName"];
|
|
readonly string downloadUrl = ConfigurationManager.AppSettings["DownloadUrl"];
|
|
string version;
|
|
public MainFrom(string[] args)
|
|
{
|
|
InitializeComponent();
|
|
|
|
// 参数验证
|
|
if (args.Length < 1)
|
|
{
|
|
MessageBox.Show("未接收到启动参数!程序即将关闭!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
Close();
|
|
}
|
|
else
|
|
{
|
|
version = args[0];
|
|
}
|
|
|
|
// 杀死业务系统进程
|
|
Process[] processes = Process.GetProcessesByName(programName);
|
|
foreach (Process process in processes)
|
|
{
|
|
process.Kill();
|
|
process.WaitForExit(); // 可选:等待进程完全退出
|
|
process.Close(); // 释放资源
|
|
}
|
|
|
|
|
|
MainBackgroundWorker.DoWork += BackgroundWorker_DoWork;
|
|
MainBackgroundWorker.ProgressChanged += BackgroundWorker_ProgressChanged;
|
|
MainBackgroundWorker.RunWorkerCompleted += BackgroundWorker_RunWorkerCompleted;
|
|
|
|
MainBackgroundWorker.RunWorkerAsync();
|
|
}
|
|
|
|
private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
|
|
{
|
|
//下载 80%
|
|
Download();
|
|
//解压 10%
|
|
UnZip();
|
|
//复制 10%
|
|
Copy();
|
|
}
|
|
|
|
private void Download()
|
|
{
|
|
//保存路径
|
|
var savePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Update");
|
|
if (Directory.Exists(savePath))
|
|
{
|
|
Directory.Delete(savePath, true);
|
|
}
|
|
Directory.CreateDirectory(savePath);
|
|
//保存文件
|
|
var zipFilePath = Path.Combine(savePath, version + ".zip");
|
|
|
|
// 创建HttpWebRequest对象
|
|
var request = (HttpWebRequest)WebRequest.Create(downloadUrl + $"?fileName={version}.zip");
|
|
// 获取响应
|
|
using (var response = (HttpWebResponse)request.GetResponse())
|
|
{
|
|
long totalBytes = response.ContentLength;
|
|
long downloadedBytes = 0;
|
|
|
|
// 读取响应流
|
|
using (var stream = response.GetResponseStream())
|
|
{
|
|
// 将响应保存到本地文件
|
|
using (var fileStream = File.Create(zipFilePath))
|
|
{
|
|
var buffer = new byte[4096];
|
|
int bytesRead;
|
|
// 循环读取和写入,直到读取完整个响应流
|
|
while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
|
|
{
|
|
// 写入文件流
|
|
fileStream.Write(buffer, 0, bytesRead);
|
|
downloadedBytes += bytesRead;
|
|
MainBackgroundWorker.ReportProgress((int)((double)downloadedBytes / totalBytes * 80), "正在下载");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void UnZip()
|
|
{
|
|
//保存路径
|
|
var savePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Update");
|
|
//保存文件
|
|
var zipFilePath = Path.Combine(savePath, version + ".zip");
|
|
//解压路径
|
|
var unZipPath = Path.Combine(savePath, "UnZip");
|
|
|
|
if (Directory.Exists(unZipPath))
|
|
Directory.Delete(unZipPath, true);
|
|
Directory.CreateDirectory(unZipPath);
|
|
|
|
|
|
int totalFiles = 0;
|
|
int processedFiles = 0;
|
|
|
|
using (ZipFile zip = ZipFile.Read(zipFilePath, new ReadOptions { Encoding = Encoding.GetEncoding("GB2312") }))
|
|
{
|
|
totalFiles = zip.Count;
|
|
|
|
foreach (ZipEntry entry in zip)
|
|
{
|
|
entry.Extract(unZipPath, ExtractExistingFileAction.OverwriteSilently);
|
|
processedFiles++;
|
|
|
|
// 报告进度
|
|
MainBackgroundWorker.ReportProgress((int)((double)processedFiles / totalFiles * 10) + 80, "正在解压");
|
|
}
|
|
}
|
|
}
|
|
|
|
private void OnExtractProgress(object sender, ExtractProgressEventArgs e)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 在 BackgroundWorker 中递归复制文件(简单进度报告)
|
|
/// </summary>
|
|
private void Copy()
|
|
{
|
|
string sourceDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Update", "UnZip", version);
|
|
string destDir = Directory.GetParent(AppDomain.CurrentDomain.BaseDirectory).Parent.FullName;
|
|
|
|
int totalFiles = CountAllFiles(sourceDir);
|
|
int copiedFiles = 0;
|
|
|
|
// 执行复制
|
|
CopyFiles(sourceDir, destDir, ref copiedFiles, totalFiles);
|
|
MainBackgroundWorker.ReportProgress(100, $"复制完成,共复制 {copiedFiles} 个文件");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 统计目录下所有文件数量
|
|
/// </summary>
|
|
private int CountAllFiles(string directory)
|
|
{
|
|
int count = 0;
|
|
|
|
try
|
|
{
|
|
// 当前目录的文件数
|
|
count += Directory.GetFiles(directory).Length;
|
|
|
|
// 递归统计子目录
|
|
foreach (string subDir in Directory.GetDirectories(directory))
|
|
{
|
|
count += CountAllFiles(subDir);
|
|
}
|
|
}
|
|
catch (UnauthorizedAccessException)
|
|
{
|
|
// 无权限访问,跳过该目录
|
|
}
|
|
|
|
return count;
|
|
}
|
|
|
|
private void CopyFiles(string sourceDir, string destDir, ref int copiedFiles, int totalFiles)
|
|
{
|
|
if (sourceDir.EndsWith("Update"))
|
|
{
|
|
return;
|
|
}
|
|
|
|
// 复制当前目录的文件
|
|
foreach (string sourceFile in Directory.GetFiles(sourceDir))
|
|
{
|
|
string destFile = Path.Combine(destDir, Path.GetFileName(sourceFile));
|
|
File.Copy(sourceFile, destFile, overwrite: true);
|
|
copiedFiles++;
|
|
|
|
// 报告进度
|
|
MainBackgroundWorker.ReportProgress((int)((double)copiedFiles / totalFiles * 10) + 90, "正在复制");
|
|
}
|
|
|
|
// 递归处理子目录
|
|
foreach (string subDir in Directory.GetDirectories(sourceDir))
|
|
{
|
|
|
|
string destSubDir = Path.Combine(destDir, Path.GetFileName(subDir));
|
|
Directory.CreateDirectory(destSubDir);
|
|
|
|
CopyFiles(subDir, destSubDir, ref copiedFiles, totalFiles);
|
|
}
|
|
}
|
|
|
|
private void BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
|
|
{
|
|
// 更新进度条
|
|
MainProgressBar.Value = e.ProgressPercentage;
|
|
|
|
// 更新百分比标签
|
|
MainProcessLabel.Text = $"{e.ProgressPercentage}%";
|
|
|
|
// 如果需要显示具体信息
|
|
//if (e.UserState != null)
|
|
//{
|
|
// lblProgress.Text = $"{e.ProgressPercentage}% - {e.UserState}";
|
|
//}
|
|
}
|
|
|
|
private void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
|
|
{
|
|
if (e.Error != null)
|
|
{
|
|
var box = new CountdownMessageBox($"更新出错, 请联系系统管理员:\r\n{e.Error.Message}", 15);
|
|
box.ShowDialog();
|
|
}
|
|
else
|
|
{
|
|
var box = new CountdownMessageBox("更新完成!即将启动系统...");
|
|
box.ShowDialog();
|
|
Process.Start(Path.Combine(Directory.GetParent(AppDomain.CurrentDomain.BaseDirectory).Parent.FullName, programName + ".exe"), version);
|
|
}
|
|
Close();
|
|
}
|
|
}
|
|
}
|
|
|