287 lines
10 KiB
C#
287 lines
10 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.IO;
|
|
using System.IO.Compression;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace Update
|
|
{
|
|
public partial class MainForm : Form
|
|
{
|
|
public string Title { get { return Text; } set { Text = value; } }
|
|
private Point mouseOffset; // 记录鼠标指针的坐标
|
|
private bool isMouseDown; // 记录鼠标左键是否按下
|
|
|
|
public MainForm()
|
|
{
|
|
InitializeComponent();
|
|
|
|
this.TopMost = true;
|
|
}
|
|
|
|
private void MessageForm_Paint(object sender, PaintEventArgs e)
|
|
{
|
|
SetFormRoundRectRgn(this, 7);
|
|
SetFormRoundRectRgn(panel2, 7);
|
|
SetFormRoundRectRgn(this.btnOk, 7);
|
|
SetFormRoundRectRgn(this.btnCancel, 7);
|
|
}
|
|
|
|
private void btnOk_Click(object sender, EventArgs e)
|
|
{
|
|
if (lblMessage.Text == "升级完成,点击确定重启程序")
|
|
{
|
|
string extPath = System.Environment.CurrentDirectory + "\\" + Program.softName.Replace(".exe", "") + ".exe";
|
|
System.Diagnostics.Process.Start(extPath);
|
|
Application.Exit();
|
|
return;
|
|
}
|
|
|
|
btnOk.Enabled = false;
|
|
|
|
CloseMainProgram();
|
|
|
|
if (!System.IO.Directory.Exists("upBak"))
|
|
System.IO.Directory.CreateDirectory("upBak");
|
|
var upBakPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "upBak";
|
|
if (!System.IO.Directory.Exists("upTemp"))
|
|
System.IO.Directory.CreateDirectory("upTemp");
|
|
var upTempPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "upTemp";
|
|
|
|
// 清空文件夹准备下载
|
|
foreach (string filePath in Directory.GetFiles(upTempPath))
|
|
{
|
|
File.Delete(filePath); // 删除文件
|
|
}
|
|
foreach (string dirPath in Directory.GetDirectories(upTempPath))
|
|
{
|
|
Directory.Delete(dirPath, true); // 删除子文件夹及其内容
|
|
}
|
|
|
|
this.lblMessage.Text = "升级中请稍候...";
|
|
progressBar1.Visible = true;
|
|
progressBar1.Maximum = 100;//fileNameArr.Count();
|
|
progressBar1.Value = 0;
|
|
new TaskFactory().StartNew(() =>
|
|
{
|
|
string tempFile = upTempPath + "\\" + Program.updateUrl.Substring(Program.updateUrl.LastIndexOf("/") + 1);
|
|
bool result = Program.DownloadFile(Program.updateUrl, tempFile);
|
|
|
|
while (true)
|
|
{
|
|
Invoke(new Action(() =>
|
|
{
|
|
progressBar1.Value = Program.progress;
|
|
}));
|
|
if (Program.progress == 100)
|
|
break;
|
|
}
|
|
|
|
if (result)
|
|
{
|
|
ZipFile.ExtractToDirectory(tempFile, upTempPath);
|
|
File.Delete(tempFile);
|
|
|
|
string mainPath = System.Environment.CurrentDirectory;
|
|
// 备份旧文件
|
|
foreach (string filePath in Directory.GetFiles(upTempPath))
|
|
{
|
|
string fileName = filePath.Substring(filePath.LastIndexOf("\\") + 1);
|
|
File.Copy(mainPath + "\\" + fileName, upBakPath + "\\" + fileName, true);
|
|
Thread.Sleep(10);
|
|
}
|
|
foreach (string dirPath in Directory.GetDirectories(upTempPath))
|
|
{
|
|
string fileName = dirPath.Substring(dirPath.LastIndexOf("\\") + 1);
|
|
CopyDirectory(mainPath + "\\" + fileName, upBakPath + "\\" + fileName);
|
|
Thread.Sleep(10);
|
|
}
|
|
|
|
string error = "";
|
|
// 替换旧文件
|
|
foreach (string filePath in Directory.GetFiles(upTempPath))
|
|
{
|
|
if (!string.IsNullOrEmpty(error))
|
|
break;
|
|
try
|
|
{
|
|
string fileName = filePath.Substring(filePath.LastIndexOf("\\") + 1);
|
|
File.Copy(upTempPath + "\\" + fileName, mainPath + "\\" + fileName, true);
|
|
Thread.Sleep(10);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
error = "发生错误:\r\n" + ex.Message + "\r\n请关闭升级窗口重试!";
|
|
Invoke(new Action(() =>
|
|
{
|
|
lblMessage.Text = error;
|
|
}));
|
|
}
|
|
}
|
|
foreach (string dirPath in Directory.GetDirectories(upTempPath))
|
|
{
|
|
if (!string.IsNullOrEmpty(error))
|
|
break;
|
|
try
|
|
{
|
|
string fileName = dirPath.Substring(dirPath.LastIndexOf("\\") + 1);
|
|
CopyDirectory(upTempPath + "\\" + fileName, mainPath + "\\" + fileName);
|
|
Thread.Sleep(10);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
error = "发生错误:\r\n" + ex.Message + "\r\n请关闭升级窗口重试!";
|
|
Invoke(new Action(() =>
|
|
{
|
|
lblMessage.Text = error;
|
|
}));
|
|
}
|
|
}
|
|
|
|
Thread.Sleep(50);
|
|
Invoke(new Action(() =>
|
|
{
|
|
if (string.IsNullOrEmpty(error))
|
|
{
|
|
progressBar1.Value = 100;
|
|
lblMessage.Text = "升级完成,点击确定重启程序";
|
|
btnOk.Enabled = true;
|
|
}
|
|
}));
|
|
}
|
|
else
|
|
{
|
|
Invoke(new Action(() =>
|
|
{
|
|
progressBar1.Value = 100;
|
|
this.lblMessage.Text = "升级失败,未获取到升级文件";
|
|
btnOk.Enabled = true;
|
|
}));
|
|
}
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// 拷贝文档目录
|
|
/// </summary>
|
|
/// <param name="srcPath"></param>
|
|
/// <param name="destPath"></param>
|
|
public static void CopyDirectory(string srcPath, string destPath)
|
|
{
|
|
try
|
|
{
|
|
DirectoryInfo dir = new DirectoryInfo(srcPath);
|
|
FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //获取目录下(不包含子目录)的文件和子目录
|
|
foreach (FileSystemInfo i in fileinfo)
|
|
{
|
|
if (i is DirectoryInfo) //判断是否文件夹
|
|
{
|
|
if (!Directory.Exists(destPath + "\\" + i.Name))
|
|
{
|
|
Directory.CreateDirectory(destPath + "\\" + i.Name); //目标目录下不存在此文件夹即创建子文件夹
|
|
}
|
|
CopyDirectory(i.FullName, destPath + "\\" + i.Name); //递归调用复制子文件夹
|
|
}
|
|
else
|
|
{
|
|
File.Copy(i.FullName, destPath + "\\" + i.Name, true); //不是文件夹即复制文件,true表示可以覆盖同名文件
|
|
}
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
private void btnCancel_Click(object sender, EventArgs e)
|
|
{
|
|
this.Close();
|
|
}
|
|
|
|
private void panel1_MouseDown(object sender, MouseEventArgs e)
|
|
{
|
|
if (e.Button == MouseButtons.Left)
|
|
{
|
|
mouseOffset = new Point(-e.X, -e.Y);
|
|
isMouseDown = true;
|
|
}
|
|
}
|
|
|
|
private void panel1_MouseUp(object sender, MouseEventArgs e)
|
|
{
|
|
isMouseDown = false;
|
|
}
|
|
|
|
private void panel1_MouseMove(object sender, MouseEventArgs e)
|
|
{
|
|
if (isMouseDown)
|
|
{
|
|
// 获取鼠标的位置
|
|
Point mousePos = MousePosition;
|
|
mousePos.Offset(mouseOffset.X, mouseOffset.Y);
|
|
Location = mousePos;
|
|
}
|
|
}
|
|
|
|
public static void SetFormRoundRectRgn(Control c, int rgnRadius)
|
|
{
|
|
int hRgn = 0;
|
|
hRgn = Win32.CreateRoundRectRgn(0, 0, c.Width + 1, c.Height + 1, rgnRadius, rgnRadius);
|
|
Win32.SetWindowRgn(c.Handle, hRgn, true);
|
|
Win32.DeleteObject(hRgn);
|
|
}
|
|
|
|
private void btnClose_Click(object sender, EventArgs e)
|
|
{
|
|
Application.Exit();
|
|
}
|
|
|
|
private void CloseMainProgram()
|
|
{
|
|
string in_sAppName = Program.softName; //(一个word文档程序)
|
|
in_sAppName = in_sAppName.Replace(".exe", "");
|
|
|
|
try
|
|
{
|
|
System.Diagnostics.Process[] psiNewcas = System.Diagnostics.Process.GetProcessesByName(in_sAppName);
|
|
//System.Diagnostics.Process.GetProcessesByName("");
|
|
|
|
//var pro = System.Diagnostics.Process.GetProcesses();
|
|
//foreach(var p in pro)
|
|
//{
|
|
// Console.WriteLine(p.ProcessName);
|
|
//}
|
|
|
|
foreach (System.Diagnostics.Process p in psiNewcas)
|
|
{
|
|
p.Kill();
|
|
//p.WaitForExit();
|
|
//p.Close();
|
|
//Console.WriteLine(in_sAppName + "已经停止了");
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
Console.WriteLine(in_sAppName + "停止失败");
|
|
}
|
|
}
|
|
}
|
|
|
|
public enum FormLevel
|
|
{
|
|
SMALL,
|
|
MEDIUM,
|
|
LARGE = 500
|
|
}
|
|
|
|
|
|
}
|