软件升级相关代码
This commit is contained in:
1 parent
93e5e36dd3
commit
892d7e1f6c
20 files changed
+1697
-3
No files matched your search
@@ -0,0 +1,231 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Data;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
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);
|
||||
}
|
||||
|
||||
private void btnOk_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (lblMessage.Text == "升级完成,点击确定重启程序")
|
||||
{
|
||||
string mainPath = System.Environment.CurrentDirectory + "\\" + Program.softName + ".exe";
|
||||
System.Diagnostics.Process.Start(mainPath);
|
||||
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";
|
||||
|
||||
|
||||
this.lblMessage.Text = "升级中请稍候...";
|
||||
progressBar1.Visible = true;
|
||||
progressBar1.Maximum = 1;//fileNameArr.Count();
|
||||
progressBar1.Value = 0;
|
||||
new TaskFactory().StartNew(() =>
|
||||
{
|
||||
bool result = Program.DownloadFile(Program.updateUrl, upTempPath + "\\" + Program.updateUrl.Substring(Program.updateUrl.LastIndexOf("/") + 1));
|
||||
|
||||
if (result)
|
||||
{
|
||||
Thread.Sleep(50);
|
||||
Invoke(new Action(() =>
|
||||
{
|
||||
lblMessage.Text = "升级完成,点击确定重启程序";
|
||||
btnOk.Enabled = true;
|
||||
}));
|
||||
}
|
||||
else
|
||||
{
|
||||
Invoke(new Action(() =>
|
||||
{
|
||||
this.lblMessage.Text = "升级失败,未获取到升级文件";
|
||||
btnOk.Enabled = true;
|
||||
}));
|
||||
}
|
||||
|
||||
//var fileNames = Program.updateUrl;
|
||||
//var fileNameArr = fileNames.Split(new string[] { ",", ";" }, StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
//if (fileNameArr.Count() > 0)
|
||||
//{
|
||||
// foreach (var path in fileNameArr)
|
||||
// {
|
||||
// Thread.Sleep(new Random().Next(500, 1000));
|
||||
|
||||
// var localPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + path.Substring(path.LastIndexOf("/") + 1);
|
||||
// var tempPath = upBakPath + "\\" + path.Substring(path.LastIndexOf("/") + 1);
|
||||
// File.Copy(localPath, tempPath, true);
|
||||
|
||||
// if (!result)
|
||||
// {
|
||||
// Invoke(new Action(() =>
|
||||
// {
|
||||
// this.lblMessage.Text = "升级失败,请稍后点击确定重试";
|
||||
// btnOk.Enabled = true;
|
||||
// }));
|
||||
// return;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// Invoke(new Action(() =>
|
||||
// {
|
||||
// if (progressBar1.Value < progressBar1.Maximum)
|
||||
// progressBar1.Value++;
|
||||
// }));
|
||||
// }
|
||||
|
||||
// }
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// 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文档程序)
|
||||
|
||||
try
|
||||
{
|
||||
System.Diagnostics.Process[] psiNewcas = System.Diagnostics.Process.GetProcessesByName(in_sAppName);
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user