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.
 
 
 
 
 

247 lines
6.6 KiB

using DemoUI.Common;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace DemoUI
{
class VersionUpdate
{
/// <summary>
/// 版本更新
/// </summary>
public static void UpdateVersion()
{
string downUrl = "";
QMLog log = new QMLog();
try
{
string filepath = AppInit.GetApp().AppConfig.SmallBackUrl + @"/update/update.xml";
//下载更新XML
WebClientPro mywebclient = new WebClientPro(5000);
string downloadPath = AppInit.GetApp().AppConfig.DownloadUrlXML + @"/temp/update.xml";
if (!Directory.Exists(AppInit.GetApp().AppConfig.DownloadUrlXML + @"/temp/"))
{
Directory.CreateDirectory(AppInit.GetApp().AppConfig.DownloadUrlXML + @"/temp/");
}
mywebclient.DownloadFile(filepath, downloadPath);
//解析更新XML,读取信息
XmlDocument xmlDoc = new XmlDocument();
string xmlString = ReadContext(downloadPath);
xmlDoc.LoadXml(xmlString);
var updateVersion = xmlDoc.SelectSingleNode("//updateVersion");
if (updateVersion != null)
{
double version = Convert.ToDouble(updateVersion.InnerXml);
double nowVersion = Convert.ToDouble(AppInit.GetApp().AppConfig.updateVersion);
if (version > nowVersion)
{
////更新App.config中的版本号
AppInit.SetConfig("updateVersion", updateVersion.InnerXml);
string fullName = AppInit.GetApp().AppConfig.CurrentPath + "../Update/update.exe";
var processInfo = new ProcessStartInfo(fullName);
processInfo.WindowStyle = ProcessWindowStyle.Hidden;
processInfo.UseShellExecute = false;
processInfo.WorkingDirectory = Path.GetDirectoryName(fullName);
Process.Start(processInfo);
Environment.Exit(0);
return;
}
}
//var list = xmlDoc.SelectNodes("//Version");
}
catch (Exception ex)
{
log.WriteLogFile("更新版本出错(" + downUrl + "):" + ex.Message + "|" + ex.StackTrace, "versionUpdate");
}
}
private static string ReadContext(string path)
{
FileStream fs = new FileStream(path, FileMode.Open);
StreamReader sr = new StreamReader(fs, System.Text.Encoding.UTF8);
string context = sr.ReadToEnd();
fs.Close();
sr.Close();
sr.Dispose();
fs.Dispose();
return context;
}
}
/// <summary>
/// 系统初始化
/// </summary>
public class AppInit
{
static AppInit _instance = null;
public Config AppConfig { get; private set; }
private AppInit()
{
InitConfig();
}
public static AppInit GetApp()
{
if (_instance == null)
{
_instance = new AppInit();
}
return _instance;
}
/// <summary>
/// 初始化配置文件
/// </summary>
public void InitConfig()
{
try
{
AppConfig = new Config();
DirectoryInfo di = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory);
//初始化配置参数,因参数暂时较少,可手动填装,若后期配置项变多,可分一个专门的JSON配置文件出来,使用JsonConvert.DeserializeObject<Config>(System.IO.File.ReadAllText(ConfigPath))进行实体填充
//AppConfig.ServiceName = ConfigurationManager.AppSettings["ServiceName"];//服务名称
//AppConfig.ServiceDescribe = ConfigurationManager.AppSettings["ServiceDescirbe"];//服务描述
//AppConfig.ApiUrl = ConfigurationManager.AppSettings["ApiUrl"];//请求的接口地址
//AppConfig.BigBackUrl = ConfigurationManager.AppSettings["BigBackUrl"];//大后台地址
AppConfig.SmallBackUrl = ConfigurationManager.AppSettings["HttpUrl"];//后台地址
AppConfig.updateVersion = ConfigurationManager.AppSettings["updateVersion"];//版本号
AppConfig.DownloadUrlXML = ConfigurationManager.AppSettings["DownloadUrlXML"];//XML下载路径
AppConfig.ParentPath = di.Parent.FullName;//当前路径
AppConfig.CurrentPath = di.FullName;//当前路径
AppConfig.UpdateReleasePath = di.Parent.FullName + ConfigurationManager.AppSettings["UpdateReleasePath"];//更新包解压路径
}
catch (Exception e)
{
throw e;
}
}
public static bool SetConfig(string Key, string Value)
{
try
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (config.AppSettings.Settings[Key] != null)
config.AppSettings.Settings[Key].Value = Value;
else
config.AppSettings.Settings.Add(Key, Value);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
return true;
}
catch
{
return false;
}
}
}
/// <summary>
/// 配置项
/// </summary>
public class Config
{
/// <summary>
/// 服务名称
/// </summary>
public string ServiceName { get; set; }
/// <summary>
/// 服务描述
/// </summary>
public string ServiceDescribe { get; set; }
/// <summary>
/// 接口地址
/// </summary>
public string ApiUrl { get; set; }
/// <summary>
/// 大后台地址
/// </summary>
public string BigBackUrl { get; set; }
/// <summary>
/// 小后台地址
/// </summary>
public string SmallBackUrl { get; set; }
/// <summary>
/// 下载图片文件夹路径
/// </summary>
public string DownloadUrlImage { get; set; }
/// <summary>
/// 正式图片文件夹路径
/// </summary>
public string FormalUrlImage { get; set; }
/// <summary>
/// 下载Json文件夹路径
/// </summary>
public string DownloadUrlJson { get; set; }
/// <summary>
/// 正式Json文件夹路径
/// </summary>
public string FormalUrlJson { get; set; }
/// <summary>
/// Json文件名
/// </summary>
public string JsonFileName { get; set; }
/// <summary>
/// 离线应用版本号
/// </summary>
public string updateVersion { get; set; }
/// <summary>
/// 版本号
/// </summary>
public string Version { get; set; }
/// <summary>
/// 版本类型(横板H/竖版S)
/// </summary>
public string VersionType { get; set; }
/// <summary>
/// 更新XML文件下载地址
/// </summary>
public string DownloadUrlXML { get; set; }
/// <summary>
/// 当前路径
/// </summary>
public string CurrentPath { get; set; }
/// <summary>
/// 父目录
/// </summary>
public string ParentPath { get; set; }
/// <summary>
/// 更新包解压路径
/// </summary>
public string UpdateReleasePath { get; set; }
/// <summary>
/// 正式DIST路径
/// </summary>
public string FormalDistPath { get; set; }
/// <summary>
/// 备份DIST路径
/// </summary>
public string BackDistPath { get; set; }
}
}