/// <summary> /// 检查更新 /// </summary> /// <returns></returns> internal static string CheckUpdate(string clientid) { WebClient wc = new WebClient(); try { wc.Headers.Add("Cache-control", "no-cache"); wc.Headers.Add("User-Agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)"); wc.Encoding = Encoding.UTF8; string url = Global.UpdateUrl + "/" + clientid; url = Uri.EscapeUriString(url); string jsonStr = wc.DownloadString(url); if (jsonStr == "null") { jsonStr = null; } jsonStr = jsonStr?.Trim('"'); if (!string.IsNullOrWhiteSpace(jsonStr) && !jsonStr.StartsWith("err_")) { OtaInfo info = JsonConvert.DeserializeObject <OtaInfo>(jsonStr.FromBase64String()); if (string.Compare(GetAppVersion(info.MainFile), info.AppVersion, StringComparison.Ordinal) >= 0) //没有更新 { jsonStr = string.Empty; } } return(jsonStr); } catch (Exception e) { return("err_" + e.Message); } }
public UpdateForm(string upinfo) { InitializeComponent(); upresult = JsonConvert.DeserializeObject <OtaInfo>(upinfo.FromBase64String()); if (upresult == null) { throw new Exception("检查更新失败,请重试!"); } else { Text += " - " + upresult.AppName; } lblFullUrl.LinkClicked += LblFullUrl_LinkClicked; }
// GET api/<controller>/5 public string Get(string id) { try { string rootPath = HostingEnvironment.MapPath("~"); string updatePath = Path.Combine(rootPath, "Update", id); if (!Directory.Exists(updatePath)) { return("err_" + "请求的路径无效"); } string[] ups = Directory.GetDirectories(updatePath, "*", SearchOption.TopDirectoryOnly); string newdir = ups.Max(); if (string.IsNullOrWhiteSpace(newdir)) { return(null); } string upStr = null; string cachePath = Path.Combine(newdir, "ota", "CacheUpdate"); if (File.Exists(cachePath)) { upStr = File.ReadAllText(cachePath); } else { OtaInfo otaInfo = new OtaInfo() { AppID = id }; otaInfo.AppName = File.ReadAllText(Path.Combine(updatePath, "Name.txt")); otaInfo.AppGUID = File.ReadAllText(Path.Combine(updatePath, "GUID.txt")); DateTime upTime = DateTime.ParseExact(Path.GetFileNameWithoutExtension(newdir), "yyyyMMddHHmmss", System.Globalization.CultureInfo.InvariantCulture); otaInfo.AppVerTime = upTime; string[] lines = File.ReadAllLines(Path.Combine(newdir, "Version.txt")); string newVersion = lines[0].Substring(3); otaInfo.AppVersion = newVersion; otaInfo.AppVerText = string.Join("\n", lines); otaInfo.MainFile = File.ReadAllText(Path.Combine(newdir, "MainFile.txt")); otaInfo.SetupUrl = LocalPath2WebPath(Path.Combine(newdir, "Setup.exe")); otaInfo.OtaFiles = new List <OtaFile>(); List <string> otas = Directory.GetFiles(Path.Combine(newdir, "ota"), "*", SearchOption.AllDirectories).ToList(); if (File.Exists(Path.Combine(rootPath, "Update", "Common", id))) { string[] commFileNames = JsonConvert.DeserializeObject <string[]>(File.ReadAllText(Path.Combine(rootPath, "Update", "Common", id), Encoding.UTF8)); for (int i = 0; i < commFileNames.Length; i++) { otas.Add(Path.Combine(rootPath, "Update", "Common", "Files", commFileNames[i])); } } for (int i = 0; i < otas.Count; i++) { string otaFilePath = otas[i]; OtaFile file = new OtaFile(); file.FileName = Path.GetFileName(otaFilePath); file.FileMd5 = GetMd5ByFilePath2(otaFilePath); file.DownloadUrl = LocalPath2WebPath(otaFilePath); string tmpPath = Path.Combine(newdir, "ota"); if (otaFilePath.StartsWith(tmpPath)) { file.RelativePath = otaFilePath.Substring(tmpPath.Length); } else { tmpPath = Path.Combine(rootPath, "Update", "Common", "Files"); if (otaFilePath.StartsWith(tmpPath)) { file.RelativePath = otaFilePath.Substring(tmpPath.Length); } } otaInfo.OtaFiles.Add(file); } upStr = JsonConvert.SerializeObject(otaInfo); upStr = upStr.ToBase64String(); File.WriteAllText(cachePath, upStr); } return(upStr); } catch (Exception e) { return("err_" + e.Message); } }