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.
168 lines
6.3 KiB
168 lines
6.3 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Windows.Forms;
|
|
|
|
namespace Update
|
|
{
|
|
public partial class Form1 : Form
|
|
{
|
|
// 当前程序目录 **/Update/
|
|
private static string CurrentPath = Application.StartupPath;
|
|
|
|
public Form1()
|
|
{
|
|
InitializeComponent();
|
|
TopMost = true;
|
|
MaximizeBox = false;
|
|
ControlBox = true;
|
|
ControlBox = false;
|
|
|
|
try
|
|
{
|
|
// 体检系统
|
|
var program = "PEIS.exe";
|
|
// 解压文件目录 **/Update/Unzip/
|
|
var unZipPath = Path.Combine(CurrentPath, "UnZip");
|
|
// 检查程序是否正在运行
|
|
var processes = Process.GetProcessesByName(program);
|
|
if (processes.Length > 0)
|
|
{
|
|
// 关闭程序
|
|
foreach (var process in processes)
|
|
{
|
|
process.Kill();
|
|
process.WaitForExit(); // 等待程序退出
|
|
Console.WriteLine($@"Closed existing instance of {program}");
|
|
}
|
|
// 等待1秒
|
|
Thread.Sleep(1000);
|
|
}
|
|
lblTips.Text = @"正在更新……";
|
|
// 删除文件夹
|
|
DelDirectory();
|
|
// 解压路径是否存在
|
|
if (Directory.Exists(unZipPath))
|
|
{
|
|
// 获取解压文件下的第一个文件夹,避免 **/Update/UnZip/PEIS.exe || **/Update/UnZip/XxDirectory/PEIS.exe
|
|
var temp = Directory.GetDirectories(unZipPath, "*", SearchOption.AllDirectories).ToList()
|
|
?.FirstOrDefault();
|
|
if (File.Exists(Path.Combine(unZipPath, program)))
|
|
{
|
|
CopyFilesRecursively(unZipPath, Path.GetDirectoryName(CurrentPath));
|
|
var sourceFilePath = Path.Combine(unZipPath, "PEIS.exe.config");
|
|
var targetFilePath = Path.Combine(Path.GetDirectoryName(CurrentPath), "PEIS.exe.config");
|
|
// 复制并替换指定文件 PEIS.exe.config
|
|
File.Copy(sourceFilePath, targetFilePath, true);
|
|
//删除压缩目录
|
|
//Directory.Delete(unZipPath, true);
|
|
lblTips.Text = (@"更新完成,请重新启动!");
|
|
//Process.Start(programPath);
|
|
}
|
|
else if (File.Exists(Path.Combine(temp, program)))
|
|
{
|
|
CopyFilesRecursively(temp, Path.GetDirectoryName(CurrentPath));
|
|
var sourceFilePath = Path.Combine(temp, "PEIS.exe.config");
|
|
var targetFilePath = Path.Combine(Path.GetDirectoryName(CurrentPath), "PEIS.exe.config");
|
|
// 复制并替换指定文件
|
|
File.Copy(sourceFilePath, targetFilePath, true);
|
|
lblTips.Text = (@"更新完成,请重新启动!");
|
|
}
|
|
else
|
|
{
|
|
lblTips.Text = (@"未找到更新程序!");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
lblTips.Text = (@"未找到更新文件!");
|
|
}
|
|
//Process.GetCurrentProcess().Kill();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
lblTips.Text = (e.Message);
|
|
//Process.GetCurrentProcess().Kill();
|
|
}
|
|
finally
|
|
{
|
|
ControlBox = true;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 文件复制替换
|
|
/// </summary>
|
|
/// <param name="sourcePath">原位置</param>
|
|
/// <param name="targetPath">目标位置</param>
|
|
private static void CopyFilesRecursively(string sourcePath, string targetPath)
|
|
{
|
|
//现在创建所有目录
|
|
foreach (var dirPath in Directory.GetDirectories(sourcePath, "*", SearchOption.AllDirectories))
|
|
{
|
|
if (dirPath == CurrentPath) continue;
|
|
if (dirPath.Contains("Update")) continue;
|
|
var dir = dirPath.Replace(sourcePath, targetPath);
|
|
Directory.CreateDirectory(dir);
|
|
}
|
|
//复制所有文件并替换所有同名文件
|
|
foreach (var newPath in Directory.GetFiles(sourcePath, "*.*", SearchOption.AllDirectories))
|
|
{
|
|
//File.Copy(newPath, newPath.Replace(sourcePath, targetPath), true);
|
|
var newFilePath = newPath.Replace(sourcePath, targetPath);
|
|
var targetDirectory = Path.GetDirectoryName(newFilePath);
|
|
|
|
if (newPath.Contains("Update.exe")) continue;
|
|
if (newFilePath.Contains("Update.exe")) continue;
|
|
// 如果目标文件夹不存在,则创建
|
|
if (!Directory.Exists(targetDirectory))
|
|
{
|
|
Directory.CreateDirectory(targetDirectory);
|
|
}
|
|
|
|
File.Copy(newPath, newFilePath, true);
|
|
}
|
|
}
|
|
|
|
|
|
private static void DelDirectory()
|
|
{
|
|
var list = new List<string>()
|
|
{
|
|
"de",
|
|
"es",
|
|
"ja",
|
|
"ru",
|
|
"nl",
|
|
"x64",
|
|
"x86",
|
|
};
|
|
list.ForEach(item =>
|
|
{
|
|
var path = Path.Combine( Path.GetDirectoryName(CurrentPath), item);
|
|
DeleteDirectory(path);
|
|
});
|
|
}
|
|
|
|
static void DeleteDirectory(string targetDir)
|
|
{
|
|
if (Directory.Exists(targetDir))
|
|
{
|
|
string[] files = Directory.GetFiles(targetDir);
|
|
foreach (string file in files)
|
|
{
|
|
File.Delete(file);
|
|
}
|
|
string[] subDirs = Directory.GetDirectories(targetDir);
|
|
foreach (string subDir in subDirs)
|
|
{
|
|
DeleteDirectory(subDir);
|
|
}
|
|
Directory.Delete(targetDir);
|
|
}
|
|
}
|
|
}
|
|
} |