biz_version.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.Threading.Tasks;
  8. using System.IO;
  9. using Newtonsoft.Json;
  10. using HXX.Scanner.Biz.Socket;
  11. using HXX.Scanner.Common;
  12. namespace HXX.Scanner.Biz
  13. {
  14. /// <summary>
  15. /// 版本升级相关
  16. /// </summary>
  17. public class biz_version
  18. {
  19. /// <summary>
  20. /// 检测是否需要升级
  21. /// </summary>
  22. /// <returns></returns>
  23. public async static Task<response_http_updateInfo> check()
  24. {
  25. return await Task.Run(() =>
  26. {
  27. try
  28. {
  29. return get_version_info();
  30. }
  31. catch (Exception ee)
  32. {
  33. LogManager.WriteLog(ee);
  34. return null;
  35. }
  36. });
  37. }
  38. /// <summary>
  39. /// 向服务器发起升级询问
  40. /// </summary>
  41. /// <returns></returns>
  42. private static response_http_updateInfo get_version_info()
  43. {
  44. //var url = "http://client.test.jkydata.cn/api/scanner/upgradeInfo?currentVersion=" + ConfigManager.GetConfig("appVersion");
  45. //var url = config_manager.Get("apiUrlBase") + "api/scanner/upgradeInfo?currentVersion=" + ConfigManager.GetConfig("appVersion");
  46. var url= config_environment.url_base + "api/scanner/upgradeInfo?currentVersion=" + ConfigManager.GetConfig("appVersion");
  47. var result = http_helper.Get(url);
  48. var entity = JsonConvert.DeserializeObject<response_http_updateInfo>(result);
  49. return entity;
  50. }
  51. /// <summary>
  52. /// 启动启动器,由启动器来复制和重启主程序
  53. /// </summary>
  54. /// <param name="fileName"></param>
  55. public static void start_starter(string fileName)
  56. {
  57. Process proc = new Process();
  58. //proc.StartInfo.Arguments = strCommandline;
  59. proc.StartInfo.UseShellExecute = false;
  60. proc.StartInfo.CreateNoWindow = true;
  61. proc.StartInfo.FileName = fileName;
  62. //proc.StartInfo.RedirectStandardInput = true;
  63. //proc.StartInfo.RedirectStandardOutput = true;
  64. //proc.StartInfo.RedirectStandardError = true;
  65. proc.Start();
  66. }
  67. /// <summary>
  68. /// 保存临时升级信息,方便重启后更新本地版本信息,因为重启成功才表示升级真正成功
  69. /// </summary>
  70. /// <param name="version"></param>
  71. public static void write_updateInfo(response_http_updateInfo version)
  72. {
  73. try
  74. {
  75. var file = System.Windows.Forms.Application.StartupPath + @"\updateInfo.txt";
  76. var txt = JsonConvert.SerializeObject(version);
  77. if (File.Exists(file))
  78. {
  79. File.Delete(file);
  80. }
  81. using(StreamWriter sw=new StreamWriter(file))
  82. {
  83. sw.Write(txt);
  84. }
  85. }
  86. catch (Exception ee)
  87. {
  88. LogManager.WriteLog(ee);
  89. }
  90. }
  91. /// <summary>
  92. /// 读取临时升级信息,更新本地版本信息
  93. /// </summary>
  94. /// <returns></returns>
  95. public static bool read_updateInfo()
  96. {
  97. bool result = false;
  98. try
  99. {
  100. var file = System.Windows.Forms.Application.StartupPath + @"\updateInfo.txt";
  101. var txt = string.Empty;
  102. if (File.Exists(file))
  103. {
  104. using (StreamReader sr = new StreamReader(file))
  105. {
  106. txt = sr.ReadToEnd();
  107. }
  108. }
  109. if (!string.IsNullOrEmpty(txt))
  110. {
  111. var entity = JsonConvert.DeserializeObject<response_http_updateInfo>(txt);
  112. if (entity != null)
  113. {
  114. if (!string.IsNullOrEmpty(entity.data.appVersion))
  115. {
  116. ConfigManager.SetConfig("appVersion", entity.data.appVersion);
  117. File.Delete(file);
  118. result = true;
  119. }
  120. }
  121. }
  122. }
  123. catch (Exception ee)
  124. {
  125. LogManager.WriteLog(ee);
  126. }
  127. return result;
  128. }
  129. }
  130. }