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 { /// /// 版本更新 /// 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; } } /// /// 系统初始化 /// 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; } /// /// 初始化配置文件 /// public void InitConfig() { try { AppConfig = new Config(); DirectoryInfo di = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory); //初始化配置参数,因参数暂时较少,可手动填装,若后期配置项变多,可分一个专门的JSON配置文件出来,使用JsonConvert.DeserializeObject(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; } } } /// /// 配置项 /// public class Config { /// /// 服务名称 /// public string ServiceName { get; set; } /// /// 服务描述 /// public string ServiceDescribe { get; set; } /// /// 接口地址 /// public string ApiUrl { get; set; } /// /// 大后台地址 /// public string BigBackUrl { get; set; } /// /// 小后台地址 /// public string SmallBackUrl { get; set; } /// /// 下载图片文件夹路径 /// public string DownloadUrlImage { get; set; } /// /// 正式图片文件夹路径 /// public string FormalUrlImage { get; set; } /// /// 下载Json文件夹路径 /// public string DownloadUrlJson { get; set; } /// /// 正式Json文件夹路径 /// public string FormalUrlJson { get; set; } /// /// Json文件名 /// public string JsonFileName { get; set; } /// /// 离线应用版本号 /// public string updateVersion { get; set; } /// /// 版本号 /// public string Version { get; set; } /// /// 版本类型(横板H/竖版S) /// public string VersionType { get; set; } /// /// 更新XML文件下载地址 /// public string DownloadUrlXML { get; set; } /// /// 当前路径 /// public string CurrentPath { get; set; } /// /// 父目录 /// public string ParentPath { get; set; } /// /// 更新包解压路径 /// public string UpdateReleasePath { get; set; } /// /// 正式DIST路径 /// public string FormalDistPath { get; set; } /// /// 备份DIST路径 /// public string BackDistPath { get; set; } } }