#region
|
|
using System;
|
using System.Diagnostics;
|
using System.IO;
|
using System.Reflection;
|
using System.Windows.Forms;
|
using CSFrameworkV5.Common;
|
using CSFrameworkV5.Core;
|
|
#endregion
|
|
namespace CSFrameworkV5.Library.CommonClass
|
{
|
/// <summary>
|
/// 版本检测工具类, LAST UPDATE:20211212
|
/// </summary>
|
public class VersionCheckNEW
|
{
|
//等待升级程序安全退出时间,单位毫秒。
|
private const int WAIT_SECONDS = 3000;
|
|
//系统常量定义 - 不可更改
|
private const string
|
Arg_CheckVersion = "CheckVersion"; // 主程序调用升级程序检查是否有版本更新
|
|
private const string Arg_MainEXECall = "MainEXECall"; // 主程序调用升级程序
|
private const string UpgraderINIFile = "UpgraderClient.ini"; //本地升级配置文件
|
|
private const string
|
UpgraderServerINIFile = "UpgraderServer.ini"; //服务端配置文件
|
|
//==========================================================================================
|
//重要变量定义!!!!!
|
|
//主程序文件名。主程序调用升级程序,必须修改此名称!
|
private const string DEF_MAIN_PROGRAM_NAME = "CSFrameworkV5.Main.exe";
|
|
//CSFramework商业版升级程序文件名
|
private const string DEF_UPGRADER_NAME = "CSFramework.AutoUpgrader.exe";
|
|
//CSFramework商业版升级程序 - 控制类的名字空间
|
private const string DEF_UPGRADER_CONTROLLER =
|
"CSFramework.AutoUpgrader.UpgraderController";
|
|
//V5.0标配升级程序文件名
|
//const string DEF_UPGRADER_NAME = "CSFrameworkV5.AutoUpgrader.exe";
|
|
//V5.0标配升级程序控制类的名字空间
|
//const string DEF_UPGRADER_CONTROLLER = "CSFramework.AutoUpgrader.Library.UpgraderController";
|
|
//
|
//==========================================================================================
|
|
/// <summary>
|
/// 自动升级
|
/// </summary>
|
public static void AutoUpgrader(ref bool exitApp)
|
{
|
var file = Application.StartupPath + @"\" + DEF_UPGRADER_NAME;
|
|
//本地没有升级程序文件,不进行自我升级, UpgraderClient.exe是通过DownloaderDB下载的
|
if (!File.Exists(file)) return;
|
|
frmWaitingEx.ShowMe(null, "正在获取最新的升级程序.......");
|
CheckUpgraderByDownloader();
|
|
frmWaitingEx.ShowMe(null, "正在获取升级包.......");
|
CheckVersion(ref exitApp);
|
}
|
|
/// <summary>
|
/// 检查版本升级程序,有新版本自动下载
|
/// </summary>
|
/// <returns>True:有新版本, False:无新版本</returns>
|
public static bool CheckUpgraderByDownloader()
|
{
|
var upgraderPath =
|
Application.StartupPath + @"\" + DEF_UPGRADER_NAME;
|
var serverVer = "";
|
|
try
|
{
|
//获取升级程序的程序集
|
var fileData = File.ReadAllBytes(upgraderPath);
|
var upgrader = Assembly.Load(fileData);
|
|
//通过.NET反射机制创建UpgraderController实例
|
var t = upgrader.GetType(DEF_UPGRADER_CONTROLLER);
|
var o = t.Assembly.CreateInstance(t.FullName);
|
var M = t.GetMethod("DownloadUpgrader");
|
|
//调用DownloadUpgrader方法
|
//参数名:destPath,传值:upgraderPath==Application.StartupPath
|
var r = M.Invoke(o, new object[] { upgraderPath });
|
|
serverVer = r.ToStringEx();
|
if (serverVer != "") //有新版本,保存配置文件
|
{
|
var ini = new IniFile(Application.StartupPath + @"\" +
|
UpgraderINIFile);
|
ini.IniWriteValue("Setup", "UpgraderFileVersion",
|
serverVer
|
.ToStringEx()); //写入版本号
|
}
|
}
|
catch (Exception ex)
|
{
|
LogUserOperate.Write(ex);
|
Msg.Warning("检查升级程序失败!\r\n" + ex.Message);
|
}
|
|
return serverVer != "";
|
}
|
|
/// <summary>
|
/// 检查更新,有更新要退出主程序
|
/// </summary>
|
/// <param name="AppExit">退出应用程序</param>
|
public static void CheckVersion(ref bool AppExit)
|
{
|
AppExit = false;
|
try
|
{
|
var upgraderPath = Path.Combine(Application.StartupPath,
|
DEF_UPGRADER_NAME);
|
|
//获取升级程序的程序集
|
var fileData = File.ReadAllBytes(upgraderPath);
|
var upgrader = Assembly.Load(fileData);
|
|
//通过.NET反射机制创建UpgraderController实例
|
var t = upgrader.GetType(DEF_UPGRADER_CONTROLLER);
|
var o = t.Assembly.CreateInstance(t.FullName);
|
|
//获取升级包数量
|
var M = t.GetMethod("GetNewlyPackageCount");
|
var ret = M.Invoke(o, null);
|
var count = ConvertEx.ToInt(ret);
|
|
frmWaitingEx.HideMe();
|
|
if (count > 0) //有新版本,升级包的文件数>0
|
{
|
AppExit = true;
|
|
//启动通用升级程序(商业版)。程序参数:MainEXECall YourERP.exe
|
Process.Start(upgraderPath,
|
Arg_MainEXECall + " " + DEF_MAIN_PROGRAM_NAME);
|
}
|
}
|
catch (Exception ex)
|
{
|
LogUserOperate.Write(ex);
|
Msg.ShowError("检查版本失败!CheckVersion:" + ex.Message);
|
}
|
}
|
|
/// <summary>
|
/// 取本地升级程序的版本号
|
/// </summary>
|
/// <returns></returns>
|
public static string GetUpgraderVersion()
|
{
|
var ini =
|
new IniFile(Application.StartupPath + @"\" + UpgraderINIFile);
|
var clientVer = ini.IniReadValue("Client", "UpgraderFileVersion");
|
return clientVer;
|
}
|
|
/// <summary>
|
/// 保存升级程序版本号
|
/// </summary>
|
/// <param name="version">版本号</param>
|
public static void SaveUpgraderVersion(string version)
|
{
|
var ini =
|
new IniFile(Application.StartupPath + @"\" + UpgraderINIFile);
|
ini.IniWriteValue("Client", "UpgraderFileVersion", version);
|
}
|
}
|
}
|