Program.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. /// <summary>
  28. /// 程序主入口
  29. /// </summary>
  30. /// <param name="args">[0]程序名称,[1]静默更新 0:否 1:是 [3] 0:程序启动检测 1:手动点击检查更新按钮(在于忽略更新的情况下,手动检测时候还是要弹出来的) </param>
  31. [STAThread]
  32. static void Main(string[] args)
  33. {
  34. if (f)
  35. {
  36. try
  37. {
  38. if (ApplicationDeployment.IsNetworkDeployed)
  39. {
  40. LogTool.AddLog("当前版本" + ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString());
  41. }
  42. CheckVersion();
  43. #region
  44. //string programName = "EapForIdle";//args[0];
  45. //string silentUpdate = "1";// args[1];
  46. //string isClickUpdate = "0";
  47. //// string localAddress = HostingEnvironment.ApplicationPhysicalPath;
  48. //string localAddress = AppDomain.CurrentDomain.BaseDirectory;
  49. //if (string.IsNullOrEmpty(programName) == false)
  50. //{
  51. // UpdateWork updateWork = null;
  52. // if (GetPidByProcessName(programName) == 0 && !startprocess())
  53. // updateWork = new UpdateWork(programName, localAddress, isClickUpdate, 1);
  54. // else
  55. // {
  56. // updateWork = new UpdateWork(programName, localAddress, isClickUpdate, 2);
  57. // }
  58. // if (updateWork.UpdateVerList.Count > 0)
  59. // {
  60. // /* 当前用户是管理员的时候,直接启动应用程序
  61. // * 如果不是管理员,则使用启动对象启动程序,以确保使用管理员身份运行
  62. // */
  63. // //获得当前登录的Windows用户标示
  64. // System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
  65. // //创建Windows用户主题
  66. // Application.EnableVisualStyles();
  67. // System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
  68. // //判断当前登录用户是否为管理员
  69. // if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator))
  70. // {
  71. // if (silentUpdate == "1")
  72. // {
  73. // updateWork.Do();
  74. // }
  75. // else
  76. // {
  77. // Application.EnableVisualStyles();
  78. // Application.SetCompatibleTextRenderingDefault(false);
  79. // Application.Run(new MainForm(updateWork));
  80. // }
  81. // }
  82. // else
  83. // {
  84. // string result = Environment.GetEnvironmentVariable("systemdrive");
  85. // if (AppDomain.CurrentDomain.BaseDirectory.Contains(result))
  86. // {
  87. // //创建启动对象
  88. // ProcessStartInfo startInfo = new ProcessStartInfo
  89. // {
  90. // //设置运行文件
  91. // FileName = System.Windows.Forms.Application.ExecutablePath,
  92. // //设置启动动作,确保以管理员身份运行
  93. // Verb = "runas",
  94. // Arguments = " " + programName + " " + silentUpdate
  95. // };
  96. // //如果不是管理员,则启动UAC
  97. // System.Diagnostics.Process.Start(startInfo);
  98. // }
  99. // else
  100. // {
  101. // if (silentUpdate == "1")
  102. // {
  103. // updateWork.Do();
  104. // }
  105. // else
  106. // {
  107. // Application.EnableVisualStyles();
  108. // Application.SetCompatibleTextRenderingDefault(false);
  109. // Application.Run(new MainForm(updateWork));
  110. // }
  111. // }
  112. // }
  113. // }
  114. //}
  115. #endregion
  116. }
  117. catch (Exception ex)
  118. {
  119. MessageBox.Show(ex.Message);
  120. }
  121. }
  122. }
  123. public static int GetPidByProcessName(string processName)
  124. {
  125. Process[] arrayProcess = Process.GetProcessesByName(processName);
  126. foreach (Process p in arrayProcess)
  127. {
  128. return p.Id;
  129. }
  130. return 0;
  131. }
  132. public static bool startprocess()
  133. {
  134. try
  135. {
  136. Process.Start(Path.Combine(AppDomain.CurrentDomain.BaseDirectory + @"Debug\", "EapForIdle.exe"));
  137. return true;
  138. }
  139. catch
  140. {
  141. return false;
  142. }
  143. }
  144. public static void CheckVersion()
  145. {
  146. try
  147. {
  148. string programName = "EapForIdle";
  149. string silentUpdate = "1";
  150. string isClickUpdate = "0";
  151. startprocess();
  152. string localAddress = AppDomain.CurrentDomain.BaseDirectory + @"Debug";
  153. if (!Directory.Exists(localAddress))
  154. {
  155. Directory.CreateDirectory(localAddress);
  156. }
  157. var files = Directory.GetFiles(localAddress, "*.*");
  158. Hashtable hashtable = new Hashtable();
  159. DirectoryInfo folder = new DirectoryInfo(localAddress);
  160. foreach (FileInfo file in folder.GetFiles("*.*"))
  161. {
  162. hashtable[file.Name] = GetMD5HashFromFile(file.FullName);
  163. }
  164. CompareVersion compareVersion = new CompareVersion();
  165. compareVersion.Versions = hashtable;
  166. string Json = JsonConvert.SerializeObject(compareVersion);
  167. string ss = ip + "CompareVersion/CheckVersionName";
  168. string result = UntilApi.HttpPost(ss, Json);
  169. EapResponse response = JsonConvert.DeserializeObject<EapResponse>(result);
  170. if (response != null)
  171. {
  172. if (response.Code == 1)
  173. {
  174. var ht = JsonConvert.DeserializeObject<Hashtable>(response.Data.ToString());
  175. if (ht.Count > 0)
  176. {
  177. UpdateWork updateWork = new UpdateWork(programName, localAddress, isClickUpdate, 1, ht);
  178. updateWork.Do();
  179. }
  180. }
  181. }
  182. }
  183. catch (Exception ex)
  184. {
  185. LogTool.AddLog(ex.ToString()+ex.StackTrace);
  186. }
  187. }
  188. public static string GetMD5HashFromFile(string fileName)
  189. {
  190. try
  191. {
  192. FileStream file = new FileStream(fileName, System.IO.FileMode.Open, FileAccess.Read);
  193. MD5 md5 = new MD5CryptoServiceProvider();
  194. byte[] retVal = md5.ComputeHash(file);
  195. file.Close();
  196. StringBuilder sb = new StringBuilder();
  197. for (int i = 0; i < retVal.Length; i++)
  198. {
  199. sb.Append(retVal[i].ToString("x2"));
  200. }
  201. return sb.ToString();
  202. }
  203. catch (Exception ex)
  204. {
  205. throw new Exception("GetMD5HashFromFile() fail,error:" + ex.Message);
  206. }
  207. }
  208. static void CheckForUpdate()
  209. {
  210. try
  211. {
  212. if (ApplicationDeployment.IsNetworkDeployed)
  213. {
  214. ApplicationDeployment ad = ApplicationDeployment.CurrentDeployment;
  215. if (ad.CheckForUpdate())
  216. {
  217. LogTool.AddLog("更新版本" + ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString());
  218. //Application.ExitThread();
  219. //Restart();
  220. ad.UpdateAsync();
  221. ad.UpdateCompleted += (s, ee) =>
  222. {
  223. if (ee.Error == null)
  224. {
  225. LogTool.AddLog("更新版本成功" + ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString());
  226. //var vserson= ApplicationDeployment.CurrentDeployment.CurrentVersion;
  227. //var vserson = Assembly.GetExecutingAssembly().GetName().Version;
  228. //ForUpdate FU = new ForUpdate(this);
  229. //FU.ShowDialog();
  230. //FU.Focus();
  231. }
  232. else
  233. {
  234. LogTool.AddLog("更新版本失败" + ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString());
  235. MessageBox.Show(ee.Error.ToString());
  236. }
  237. };
  238. }
  239. else
  240. {
  241. LogTool.AddLog("不需要更新版本");
  242. }
  243. }
  244. }
  245. catch (Exception ex)
  246. {
  247. LogTool.AddLog(ex.Message);
  248. }
  249. }
  250. }
  251. }