| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- using System.IO;
- using Newtonsoft.Json;
- using HXX.Scanner.Biz.Socket;
- using HXX.Scanner.Common;
- namespace HXX.Scanner.Biz
- {
- /// <summary>
- /// 版本升级相关
- /// </summary>
- public class biz_version
- {
- /// <summary>
- /// 检测是否需要升级
- /// </summary>
- /// <returns></returns>
- public async static Task<response_http_updateInfo> check()
- {
- return await Task.Run(() =>
- {
- try
- {
- return get_version_info();
- }
- catch (Exception ee)
- {
- LogManager.WriteLog(ee);
- return null;
- }
- });
- }
- /// <summary>
- /// 向服务器发起升级询问
- /// </summary>
- /// <returns></returns>
- private static response_http_updateInfo get_version_info()
- {
- //var url = "http://client.test.jkydata.cn/api/scanner/upgradeInfo?currentVersion=" + ConfigManager.GetConfig("appVersion");
- //var url = config_manager.Get("apiUrlBase") + "api/scanner/upgradeInfo?currentVersion=" + ConfigManager.GetConfig("appVersion");
- var url= config_environment.url_base + "api/scanner/upgradeInfo?currentVersion=" + ConfigManager.GetConfig("appVersion");
- var result = http_helper.Get(url);
- var entity = JsonConvert.DeserializeObject<response_http_updateInfo>(result);
- return entity;
- }
- /// <summary>
- /// 启动启动器,由启动器来复制和重启主程序
- /// </summary>
- /// <param name="fileName"></param>
- public static void start_starter(string fileName)
- {
- Process proc = new Process();
- //proc.StartInfo.Arguments = strCommandline;
- proc.StartInfo.UseShellExecute = false;
- proc.StartInfo.CreateNoWindow = true;
- proc.StartInfo.FileName = fileName;
- //proc.StartInfo.RedirectStandardInput = true;
- //proc.StartInfo.RedirectStandardOutput = true;
- //proc.StartInfo.RedirectStandardError = true;
- proc.Start();
- }
- /// <summary>
- /// 保存临时升级信息,方便重启后更新本地版本信息,因为重启成功才表示升级真正成功
- /// </summary>
- /// <param name="version"></param>
- public static void write_updateInfo(response_http_updateInfo version)
- {
- try
- {
- var file = System.Windows.Forms.Application.StartupPath + @"\updateInfo.txt";
- var txt = JsonConvert.SerializeObject(version);
- if (File.Exists(file))
- {
- File.Delete(file);
- }
- using(StreamWriter sw=new StreamWriter(file))
- {
- sw.Write(txt);
- }
- }
- catch (Exception ee)
- {
- LogManager.WriteLog(ee);
- }
- }
- /// <summary>
- /// 读取临时升级信息,更新本地版本信息
- /// </summary>
- /// <returns></returns>
- public static bool read_updateInfo()
- {
- bool result = false;
- try
- {
- var file = System.Windows.Forms.Application.StartupPath + @"\updateInfo.txt";
- var txt = string.Empty;
- if (File.Exists(file))
- {
- using (StreamReader sr = new StreamReader(file))
- {
- txt = sr.ReadToEnd();
- }
- }
- if (!string.IsNullOrEmpty(txt))
- {
- var entity = JsonConvert.DeserializeObject<response_http_updateInfo>(txt);
- if (entity != null)
- {
- if (!string.IsNullOrEmpty(entity.data.appVersion))
- {
- ConfigManager.SetConfig("appVersion", entity.data.appVersion);
- File.Delete(file);
- result = true;
- }
- }
- }
- }
- catch (Exception ee)
- {
- LogManager.WriteLog(ee);
- }
- return result;
- }
- }
- }
|