#region
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using CSFrameworkV5.Common.Tools;
using CSFrameworkV5.Core;
#endregion
namespace CSFrameworkV5.Library.CommonForms
{
///
/// 自动检测升级包倒计时界面
///
public partial class frmAutoUpgraderWaiting : Form
{
private const int DEF_REMAINING = 5; //关闭当前窗体读秒
private const int DEF_AUTO_DETECT = 3000; //3秒启动,测试
private static Timer _AutoUpgraderTimer = new Timer();
private static Form _Owner;
//剩余读秒
private int _Remaining = DEF_REMAINING;
private frmAutoUpgraderWaiting()
{
InitializeComponent();
}
private static string GetMsg(int second)
{
return "系统检测到有新版本下载,程序将在 " + second + " 秒后关闭,请重新运行程序。";
}
private static bool IsForceUpdate()
{
var ini =
Application.StartupPath +
"\\UpgraderClient.ini"; //客户端本地配置INI文件
if (File.Exists(ini))
{
var file = new IniFile(ini);
return file.IniReadValue("Setup", "ForceUpdate") == "Y";
}
return false;
}
///
/// 外部统一接口, 启动自动检测升级包
///
public static void StartAutoUpgrading(Form owner)
{
if (SystemSettings.Current.CheckVersion && IsForceUpdate())
{
_Owner = owner;
_AutoUpgraderTimer.Interval = DEF_AUTO_DETECT;
_AutoUpgraderTimer.Tick += Timer_Tick;
_AutoUpgraderTimer.Start();
}
}
///
/// 检查升级包的时钟
///
///
///
private static void Timer_Tick(object sender, EventArgs e)
{
if (UpgraderReflection.GetPackages() > 0)
{
_AutoUpgraderTimer.Stop();
var form = new frmAutoUpgraderWaiting();
form.lblMsg.Text = GetMsg(form._Remaining);
form.timer1.Start();
form.ShowDialog(_Owner);
}
}
///
/// 界面的时钟(倒计时关闭程序)
///
///
///
private void timer1_Tick(object sender, EventArgs e)
{
_Remaining--;
lblMsg.Text = GetMsg(_Remaining);
if (_Remaining == 0)
{
_Remaining = DEF_REMAINING;
//强制退出, 忽略frmMain窗体的Form.Closing事件。
Application.ExitThread();
}
}
}
///
/// 文件读取version.xml
///
public class VersionHistory
{
///
/// 版本历史文件
///
private const string XML_FILE = @"\version.xml";
private string _CurrentVersionID;
private List _VersionHistory;
public VersionHistory()
{
_VersionHistory = new List();
}
///
/// 用户的最新版本
///
public string CurrentVersionID
{
get => _CurrentVersionID;
set => _CurrentVersionID = value;
}
///
/// 升级历史记录
///
public List History
{
get => _VersionHistory;
set => _VersionHistory = value;
}
///
/// 获取历史记录, 反序列化
///
///
public static VersionHistory GetVersionHistory()
{
var versionFile = Application.StartupPath + XML_FILE;
if (File.Exists(versionFile))
{
var o = MyXmlSerializer.Deserialize(typeof(VersionHistory),
versionFile);
return o as VersionHistory;
}
return new VersionHistory();
}
}
///
/// 自动升级程序.NET反射器
///
public static class UpgraderReflection
{
///
/// 获取升级包数据。通过.NET反射,调用升级程序的方法
///
///
public static int GetPackages()
{
try
{
//获取本地最新升级包的版本号
var v = VersionHistory.GetVersionHistory();
var newVersion = v == null ? "" : v.CurrentVersionID;
//价值升级程序的程序集
var file = Path.Combine(Application.StartupPath,
"CSFramework.AutoUpgrader.exe");
var bs = File.ReadAllBytes(file);
var asm = Assembly.Load(bs);
//获取一个程序类型
var T = asm.GetType(
"CSFramework.AutoUpgrader.UpgraderController");
var o = Activator.CreateInstance(T);
//反射一个方法
var mi = T.GetMethod("GetUpgraderPackages");
var i = mi.Invoke(o,
new object[]
{ "E81FFE4E-D0C5-430B-A558-9ECFC0F2FF0D", newVersion });
if (i == null) return 0;
return Convert.ToInt32(i);
}
catch (Exception ex)
{
//Common.Msg.Warning("反射升级程序的GetPackages方法错误!\r\n" + ex.Message);
return 0;
}
}
}
}