C/S WinForm自动升级
这二天刚好完成一个C/S 自动升级的功能 代码分享一下
/// <summary>
/// 版本检测
/// </summary>
public class VersionCheckNEW
{
private const string Arg_CheckVersion = "CheckVersion";// 主程序调用升级程序检查是否有版本更新
private const string Arg_MainEXECall = "MainEXECall";// 主程序调用升级程序
private const string UpgraderINIFile = "UpgraderClient.ini";//本地升级配置文件
private const string UpgraderServerINIFile = "UpgraderServer.ini";//服务端配置文件
/// <summary>
/// 自动升级
/// </summary>
public static void AutoUpgrader(ref bool exitApp)
{
string file = Application.StartupPath + @"\" + Globals.DEF_UPGRADER_NAME;
if (!File.Exists(file)) return;//本地没有升级程序文件,不进行自动升级
frmWaiting.ShowMe(null, "正在检查升级程序的版本...");
//检查并下载最新的版本升级程序
VersionCheckNEW.CheckUpgraderByDownloader();
frmWaiting.ShowMe(null, "正在检查系统更新,请稍候...");
//检查系统版本
VersionCheckNEW.CheckVersion(ref exitApp);
}
/// <summary>
/// 取本地升级程序的版本号
/// </summary>
/// <returns></returns>
public static string GetUpgraderVersion()
{
IniFile ini = new IniFile(Application.StartupPath + @"\" + UpgraderINIFile);
string clientVer = ini.IniReadValue("Client", "UpgraderFileVersion");
return clientVer;
}
/// <summary>
/// 保存升级程序版本号
/// </summary>
/// <param name="version">版本号</param>
public static void SaveUpgraderVersion(string version)
{
IniFile ini = new IniFile(Application.StartupPath + @"\" + UpgraderINIFile);
ini.IniWriteValue("Client", "UpgraderFileVersion", version);
}
/// <summary>
/// 检查版本升级程序,有新版本自动下载
/// </summary>
public static bool CheckUpgraderByDownloader()
{
string upgraderPath = Application.StartupPath + @"\" + Globals.DEF_UPGRADER_NAME;
string serverVer = "";
try
{
byte[] fileData = File.ReadAllBytes(upgraderPath);
Assembly upgrader = Assembly.Load(fileData);
Type t = upgrader.GetType("A8ERP.UpgraderClient.Library.UpgraderController");
object o = t.Assembly.CreateInstance(t.FullName);
MethodInfo M = t.GetMethod("DownloadUpgrader");
//调用DownloadUpgrader方法
//参数名:destPath,传值:upgraderPath==Application.StartupPath
object r = M.Invoke(o, new object[] { upgraderPath });
serverVer = r.ToString();
if (serverVer != "")//有新版本
{
IniFile ini = new IniFile(Application.StartupPath + @"\" + UpgraderINIFile);
ini.IniWriteValue("Setup", "UpgraderFileVersion", serverVer.ToString());//写入版本号
}
}
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
{
string checkResultFile = Application.StartupPath + "\\" + Guid.NewGuid().ToString().Replace("-", "") + ".ver"; //存储版本号的临时文件
string upgrader = Application.StartupPath + "\\" + Globals.DEF_UPGRADER_NAME;//升级程序文件名
File.WriteAllText(checkResultFile, ""); //创建空文件
checkResultFile = ShellPathNameConvertNEW.ToShortPathName(checkResultFile); //转换为DOS短文件名
Process pro = new Process();
pro.StartInfo = new ProcessStartInfo(upgrader, Arg_CheckVersion + " " + checkResultFile);//获取升级包的文件数
pro.StartInfo.UseShellExecute = false;
pro.Start();
pro.WaitForExit(3000);//等待升级程序关联程序退出
frmWaiting.HideMe(null);
//如果生成临时文件,返回可更新的文件数。
if (File.Exists(checkResultFile))
{
//读取临时文件内容
string result = File.ReadAllText(checkResultFile);
File.Delete(checkResultFile); //删除临时文件
if ((result != "") && (int.Parse(result) > 0))//有新版本,升级包的文件数>0
{
AppExit = true;
Process.Start(upgrader, Arg_MainEXECall); //启动自动升级程序
}
}
}
catch (Exception ex)
{
//LogUserOperate.Write(ex);
//Msg.ShowError("CheckVersion:" + ex.Message);
}
}
}