using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
//----------------------------------------------------------------
//Module Name: $safeprojectname$
//Purpose:
//CopyRight: Muchinfo
//History:
//----------------------------------------------------------------
//DateTime 2016/1/5 15:51:07
//Author
//Description Create
//----------------------------------------------------------------
using System.Windows;
using System.Windows.Controls;
namespace Muchinfo.MTPClient.Update
{
public class UpdateManage
{
private const string resource = "Config";
private const string DefaultFolder = "DefaultFolder";
private const string c_chartUpdateFile = "FinancePrice.dll"; ////图表控件
private const string c_chartRegitFile = "regchart.bat";///注册图表控件的文件路径
///图表路径目录
public static string ChartPathDir
{
get
{
return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "COM");
}
}
///
/// 是否有图表更新
///
public static bool IsChartUpdate { get; set; }
///
/// 执行判断更新
///
/// 更新服务的路径
public static UpdateInfo RunUpdate(string updatePath, string memberCode)
{
UpdateInfo remoteInfo;
string parentPath = AppDomain.CurrentDomain.BaseDirectory;
var localFileInfo = LoadLocalConfig();
bool mainUpdate = CheckMainAppUpdate(updatePath, memberCode, localFileInfo, out remoteInfo);
if (mainUpdate)
{
var tipWindow = new TipsWindow("检测到交易端有新版本是否更新?");
var result = tipWindow.ShowDialog();
// var result = MessageBox.Show(" 检测到交易端有新版本是否更新?","更新提示" , MessageBoxButton.YesNo);
if (result == true)
{
var processes = Process.GetProcessesByName(remoteInfo.StartAppName.Replace(".exe", string.Empty));
foreach (var process in processes)
{
process.Kill();
}
return remoteInfo;
}
}
return null;
}
///
/// 加载本地配置文件,如不存在则生成
///
///
public static UpdateInfo LoadLocalConfig()
{
var updateHelper = new UpdateHelper();
UpdateInfo updateInfo;
string parentPath = AppDomain.CurrentDomain.BaseDirectory;
var configPath = string.Format("{0}\\{1}.xml", parentPath, typeof(UpdateInfo).Name);
if (File.Exists(configPath)) //本地更新配置文件存在
{
updateInfo =
updateHelper.LoadUpdateConfig(configPath); //todo:读取失败应生成
}
else
{
updateInfo = updateHelper.CreateUpdateInfoFile(parentPath);
}
return updateInfo;
}
///
/// 检查主程序有没有更新
///
/// 更新基目录文件
/// 是否有本地配置
/// 服务端更新配置
///
public static bool CheckMainAppUpdate(string updatePath, string memberCode, UpdateInfo localfileInfo, out UpdateInfo updateFileInfo)
{
updateFileInfo = new UpdateInfo();
var updateHelper = new UpdateHelper();
var remoteFileInfo = updateHelper.LoadUpdateConfig(string.Format("{0}/{1}.xml", updatePath, typeof(UpdateInfo).Name));
if (remoteFileInfo.UpdateFiles == null || !remoteFileInfo.UpdateFiles.Any())
{
return false;
}
var memberPath = string.Empty;
if (!string.IsNullOrEmpty(remoteFileInfo.ResourceDir)) ////没有个性化资源
{
if (string.IsNullOrWhiteSpace(memberCode)) ////没有会员代码
{
var resourceDir = Path.Combine(remoteFileInfo.ResourceDir, ""); ////过滤会员资源
remoteFileInfo.UpdateFiles =
remoteFileInfo.UpdateFiles.Where((item) => item.FilePath.StartsWith(resourceDir)).ToList();
}
else
{
memberPath = Path.Combine(remoteFileInfo.ResourceDir, memberCode); ////取会员资源
var updateFiles = new List();
foreach (var item in updateFileInfo.UpdateFiles)
{
if (item.FilePath.StartsWith(memberPath))
{
updateFiles.Add(item);
}
}
remoteFileInfo.UpdateFiles = updateFiles;
}
}
if (localfileInfo == null || localfileInfo.UpdateFiles == null || !localfileInfo.UpdateFiles.Any()) ////本地没有文件
{
updateFileInfo = remoteFileInfo;
return true;
}
else
{
////todo:比较文件MD5。生成升级的文件列表
remoteFileInfo.UpdateFiles = CompareUpdateFile(remoteFileInfo, localfileInfo, memberPath);
updateFileInfo = remoteFileInfo;
#if DEBUG
UpdateHelper.SaveStaticUpdateXml(updateFileInfo, AppDomain.CurrentDomain.BaseDirectory + "\\test.xml");
#endif
return remoteFileInfo.UpdateFiles.Any(); ////是否有更新的文件
}
}
///
/// /
///
///
///
/// 会员代码前的升级文件
///
public static List CompareFileResourceUpdate(UpdateInfo serverInfo, UpdateInfo localinfo,
string memberPath)
{
var localFilesInfo = localinfo.UpdateFiles.ToDictionary((file) => file.FilePath);
var serverfilesInfo = serverInfo.UpdateFiles.ToDictionary((item) => item.FilePath);
var updateFiles = new List();
foreach (var fileKey in serverfilesInfo.Keys)
{
if (localFilesInfo.ContainsKey(fileKey))
{
if (!serverfilesInfo[fileKey].MD5Code.Equals(localFilesInfo[fileKey].MD5Code,StringComparison.InvariantCultureIgnoreCase))
{
updateFiles.Add(serverfilesInfo[fileKey]);
}
}
else
{
updateFiles.Add(serverfilesInfo[fileKey]);
}
}
return updateFiles;
}
///
/// 比较更新的文件
///
public static List CompareUpdateFile(UpdateInfo serverInfo, UpdateInfo localinfo, string memberPath)
{
var localFilesInfo = localinfo.UpdateFiles.ToDictionary((file) => file.FilePath.ToLower());
var serverfilesInfo = serverInfo.UpdateFiles.ToDictionary((item) => item.FilePath.ToLower());
////移除不更新的文件
var keyItems= serverfilesInfo.Keys.ToList();
var unUpdateList = UnUpdateList();
try
{
foreach (var unFile in unUpdateList)
{
foreach (var keyItem in keyItems)
{
var keys = keyItem.Split('\\');
if (keys.Any())
{
var fileNameExit = keys[keys.Length - 1];
var fileName = fileNameExit.Substring(0, fileNameExit.LastIndexOf('.'));
if (fileName.Equals(unFile))
{
serverfilesInfo.Remove(keyItem);
continue;
}
}
}
}
}
catch (Exception ex)
{
}
var updateFiles = new List();
#if DEBUG
UpdateHelper.SaveStaticUpdateXml(localinfo, AppDomain.CurrentDomain.BaseDirectory + "\\localinfo.xml");
UpdateHelper.SaveStaticUpdateXml(serverInfo, AppDomain.CurrentDomain.BaseDirectory + "\\serverInfo.xml");
#endif
foreach (var fileKey in serverfilesInfo.Keys)
{
var keyItem = fileKey.ToLower();
if (!string.IsNullOrWhiteSpace(memberPath))
{
keyItem = fileKey.Replace(memberPath, string.Empty); ////会员资源与本地的路径要一致
}
if (localFilesInfo.ContainsKey(keyItem.ToLower()))
{
if (!serverfilesInfo[keyItem].MD5Code.Equals(localFilesInfo[keyItem].MD5Code, StringComparison.InvariantCultureIgnoreCase))
{
updateFiles.Add(serverfilesInfo[keyItem]);
bool chartUpdate = keyItem.Contains(c_chartUpdateFile.ToLower());
if (chartUpdate)
{
IsChartUpdate = true; ////有图表控件更新
}
}
}
else
{
bool chartUpdate = keyItem.Contains(c_chartUpdateFile.ToLower());
if (chartUpdate)
{
IsChartUpdate = true; ////有图表控件更新
}
updateFiles.Add(serverfilesInfo[keyItem]);
}
}
return updateFiles;
}
///
/// 计算文件MD5
///
///
///
public static string GetFileMD5Code(FileInfo fileInfo)
{
using (FileStream fs = File.OpenRead(fileInfo.FullName)) //new FileStream(fileInfo.FullName, FileMode.Open))
{
System.Security.Cryptography.MD5 md5 = new MD5CryptoServiceProvider();
byte[] retval = md5.ComputeHash(fs);
fs.Close();
var sbBuilder = new StringBuilder();
for (int i = 0; i < retval.Length; i++)
{
sbBuilder.Append(retval[i].ToString("x2"));
}
return sbBuilder.ToString().ToUpper();
}
}
///
/// 注册图表控件
///
public static void RegisterChart()
{
if (IsChartUpdate)
{
var regPath = Path.Combine(ChartPathDir, c_chartRegitFile);
if (!File.Exists(regPath))
{
var chartfile = Path.Combine(ChartPathDir, c_chartUpdateFile);
var sbuilder=new StringBuilder();
sbuilder.AppendLine(string.Format("regsvr32 \"{0}\" /u", chartfile));
sbuilder.AppendLine(string.Format("regsvr32 \"{0}\" ", chartfile));
using (var fsStream = new FileStream(regPath,FileMode.CreateNew))
{
var text = new StreamWriter(fsStream, Encoding.Default);
text.Write(sbuilder.ToString());
text.Close();
fsStream.Close();
}
}
var startInfo = new System.Diagnostics.ProcessStartInfo(regPath);
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
//startInfo.StandardOutputEncoding = Encoding.UTF8;
Process process = Process.Start(startInfo);
process.WaitForExit(7000);
// Process process = Process.Start(regPath);
}
}
///
///不检查更新的文件列表
///
///
public static List UnUpdateList()
{
var updateList = new List();
updateList.Add("contresxs");
updateList.Add("errorcodes");
return updateList;
}
}
}