Program.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows.Forms;
  4. using System.IO;
  5. using System.Net;
  6. using System.Diagnostics;
  7. using System.Threading;
  8. using System.Xml;
  9. using System.Web.Hosting;
  10. using System.Deployment.Application;
  11. using System.Reflection;
  12. using System.Security.Cryptography;
  13. using System.Text;
  14. using System.Collections;
  15. using Newtonsoft.Json;
  16. using System.Configuration;
  17. using MAutoUpdate.common;
  18. using MAutoUpdate.Models;
  19. namespace MAutoUpdate
  20. {
  21. static class Program
  22. {
  23. static bool f;
  24. static Process pCurrent = Process.GetCurrentProcess();
  25. static Mutex m = new Mutex(true, pCurrent.MainModule.FileName.Replace(":", "").Replace(@"\", "") + "MAutoUpdate", out f);//互斥,
  26. static string ip = ConfigurationManager.AppSettings["IP"].ToString();
  27. static string appPath = AppDomain.CurrentDomain.BaseDirectory;
  28. static int updateCheckInterval = Convert.ToInt32(ConfigurationManager.AppSettings["UpdateCheckInterval"]) * 60;
  29. /// <summary>
  30. /// 程序主入口
  31. /// </summary>
  32. /// <param name="args">[0]程序名称,[1]静默更新 0:否 1:是 [3] 0:程序启动检测 1:手动点击检查更新按钮(在于忽略更新的情况下,手动检测时候还是要弹出来的) </param>
  33. [STAThread]
  34. static void Main(string[] args)
  35. {
  36. if (IsRunning())
  37. {
  38. LogTool.AddLog("已存在正在运行的更新程序");
  39. }
  40. else
  41. {
  42. LogTool.AddLog("正在启动定时更新程序");
  43. new EAPClock().AutoStartEap();
  44. DoCheckVersion();
  45. StartTimer();
  46. while (true)
  47. {
  48. Thread.Sleep(1000 * 60*60);
  49. }
  50. }
  51. }
  52. /// <summary>
  53. /// 是否已启动
  54. /// </summary>
  55. /// <returns></returns>
  56. static bool IsRunning()
  57. {
  58. Process[] processes = Process.GetProcesses();
  59. Process currentProcess = Process.GetCurrentProcess();
  60. bool processExist = false;
  61. foreach (Process p in processes)
  62. {
  63. if (p.ProcessName == currentProcess.ProcessName && p.Id != currentProcess.Id)
  64. {
  65. processExist = true;
  66. }
  67. }
  68. return processExist;
  69. }
  70. public static int GetPidByProcessName(string processName)
  71. {
  72. Process[] arrayProcess = Process.GetProcessesByName(processName);
  73. foreach (Process p in arrayProcess)
  74. {
  75. return p.Id;
  76. }
  77. return 0;
  78. }
  79. /// <summary>
  80. /// 检查版本更新
  81. /// </summary>
  82. public static void DoCheckVersion()
  83. {
  84. if (f)
  85. {
  86. try
  87. {
  88. if (ApplicationDeployment.IsNetworkDeployed)
  89. {
  90. LogTool.AddLog("当前版本" + ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString());
  91. }
  92. CheckVersion();
  93. }
  94. catch (Exception ex)
  95. {
  96. MessageBox.Show(ex.Message);
  97. }
  98. }
  99. }
  100. /// <summary>
  101. /// 启动定时器
  102. /// </summary>
  103. public static void StartTimer()
  104. {
  105. System.Timers.Timer timer = new System.Timers.Timer();
  106. timer.Enabled = true;
  107. //设置执行一次(false)还是一直执行(true)
  108. timer.AutoReset = true;
  109. timer.Interval = updateCheckInterval * 1000;
  110. timer.Elapsed += delegate
  111. {
  112. DoCheckVersion();
  113. };
  114. timer.Start();
  115. }
  116. public static bool startprocess()
  117. {
  118. try
  119. {
  120. Process.Start(Path.Combine(AppDomain.CurrentDomain.BaseDirectory + @"Debug\", "EapForIdle.exe"));
  121. return true;
  122. }
  123. catch
  124. {
  125. return false;
  126. }
  127. }
  128. public static void CheckVersion()
  129. {
  130. try
  131. {
  132. if (!RequireUpdate())
  133. return;
  134. LogTool.AddLog("*******************开始更新****************");
  135. string programName = "EapForIdle";
  136. string isClickUpdate = "0";
  137. string localAddress = appPath;
  138. if (!Directory.Exists(localAddress))
  139. {
  140. Directory.CreateDirectory(localAddress);
  141. }
  142. var files = Directory.GetFiles(localAddress, "*.*");
  143. Hashtable hashtable = new Hashtable();
  144. DirectoryInfo folder = new DirectoryInfo(localAddress);
  145. foreach (FileInfo file in folder.GetFiles("*.*"))
  146. {
  147. hashtable[file.Name] = GetMD5HashFromFile(file.FullName);
  148. }
  149. CompareVersion compareVersion = new CompareVersion();
  150. compareVersion.Versions = hashtable;
  151. string Json = JsonConvert.SerializeObject(compareVersion);
  152. string ss = ip + "CompareVersion/DownloadAppFile";
  153. string result = UntilApi.HttpPost(ss, Json);
  154. EapResponse response = JsonConvert.DeserializeObject<EapResponse>(result);
  155. if (response != null)
  156. {
  157. if (response.Code == 1)
  158. {
  159. var ht = JsonConvert.DeserializeObject<Hashtable>(response.Data.ToString());
  160. if (ht.Count > 0)
  161. {
  162. UpdateWork updateWork = new UpdateWork(programName, localAddress, isClickUpdate, 1, ht);
  163. updateWork.Do();
  164. }
  165. }
  166. }
  167. }
  168. catch (Exception ex)
  169. {
  170. LogTool.AddLog(ex.ToString() + ex.StackTrace);
  171. }
  172. }
  173. /// <summary>
  174. /// 检测APP师傅需要更新
  175. /// </summary>
  176. /// <returns></returns>
  177. public static bool RequireUpdate()
  178. {
  179. var versionPath = Path.Combine(appPath, "version.json");
  180. if (!File.Exists(versionPath))
  181. return true;
  182. WebAppVersion appVersion = null;
  183. var file = new FileInfo(versionPath);
  184. using (var sr = file.OpenText())
  185. {
  186. var str = sr.ReadToEnd();
  187. appVersion = JsonConvert.DeserializeObject<WebAppVersion>(str);
  188. }
  189. if (appVersion == null)
  190. return true;
  191. try
  192. {
  193. string url = ip + "CompareVersion/GetAppVersion";
  194. string result = UntilApi.HttpGet(url);
  195. EapResponse response = JsonConvert.DeserializeObject<EapResponse>(result);
  196. if (response != null && response.Code == 1)
  197. {
  198. var hs = JsonConvert.DeserializeObject<Hashtable>(response.Data.ToString());
  199. var serverAppVersion = hs["version"];
  200. LogTool.AddLog("**************检查版本开始***************");
  201. LogTool.AddLog("服务端最新版本:" + serverAppVersion + ",本地版本:" + appVersion.Version);
  202. LogTool.AddLog("**************检查版本结束***************");
  203. if (serverAppVersion.ToString() != appVersion.Version)
  204. return true;
  205. }
  206. return false;
  207. }
  208. catch (Exception ex)
  209. {
  210. LogTool.AddLog(ex.ToString() + ex.StackTrace);
  211. return false;
  212. }
  213. }
  214. public static string GetMD5HashFromFile(string fileName)
  215. {
  216. try
  217. {
  218. FileStream file = new FileStream(fileName, System.IO.FileMode.Open, FileAccess.Read);
  219. MD5 md5 = new MD5CryptoServiceProvider();
  220. byte[] retVal = md5.ComputeHash(file);
  221. file.Close();
  222. StringBuilder sb = new StringBuilder();
  223. for (int i = 0; i < retVal.Length; i++)
  224. {
  225. sb.Append(retVal[i].ToString("x2"));
  226. }
  227. return sb.ToString();
  228. }
  229. catch (Exception ex)
  230. {
  231. throw new Exception("GetMD5HashFromFile() fail,error:" + ex.Message);
  232. }
  233. }
  234. static void CheckForUpdate()
  235. {
  236. try
  237. {
  238. if (ApplicationDeployment.IsNetworkDeployed)
  239. {
  240. ApplicationDeployment ad = ApplicationDeployment.CurrentDeployment;
  241. if (ad.CheckForUpdate())
  242. {
  243. LogTool.AddLog("更新版本" + ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString());
  244. //Application.ExitThread();
  245. //Restart();
  246. ad.UpdateAsync();
  247. ad.UpdateCompleted += (s, ee) =>
  248. {
  249. if (ee.Error == null)
  250. {
  251. LogTool.AddLog("更新版本成功" + ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString());
  252. //var vserson= ApplicationDeployment.CurrentDeployment.CurrentVersion;
  253. //var vserson = Assembly.GetExecutingAssembly().GetName().Version;
  254. //ForUpdate FU = new ForUpdate(this);
  255. //FU.ShowDialog();
  256. //FU.Focus();
  257. }
  258. else
  259. {
  260. LogTool.AddLog("更新版本失败" + ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString());
  261. MessageBox.Show(ee.Error.ToString());
  262. }
  263. };
  264. }
  265. else
  266. {
  267. LogTool.AddLog("不需要更新版本");
  268. }
  269. }
  270. }
  271. catch (Exception ex)
  272. {
  273. LogTool.AddLog(ex.Message);
  274. }
  275. }
  276. }
  277. }