e204f9faecdf01f89df8c0bfb1ceb6492ae74970.svn-base 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Runtime.InteropServices;
  5. using System.IO;
  6. using System.Collections;
  7. using IWshRuntimeLibrary;
  8. using System.Windows.Forms;
  9. using System.Diagnostics;
  10. using Core.Mes.Common;
  11. using System.Net;
  12. using System.Configuration;
  13. using System.Data;
  14. namespace Core.Mes.ClientManager
  15. {
  16. class UpdaterManager
  17. {
  18. [DllImport("shfolder.dll", CharSet = CharSet.Auto)]
  19. private static extern int SHGetFolderPath(IntPtr hwndOwner, int nFolder, IntPtr hToken, int dwFlags, StringBuilder lpszPath);
  20. private const int MAX_PATH = 260;
  21. private const int CSIDL_COMMON_DESKTOPDIRECTORY = 0x0019;
  22. public static string GetAllUsersDesktopFolderPath()
  23. {
  24. StringBuilder sbPath = new StringBuilder(MAX_PATH);
  25. SHGetFolderPath(IntPtr.Zero, CSIDL_COMMON_DESKTOPDIRECTORY, IntPtr.Zero, 0, sbPath);
  26. return sbPath.ToString();
  27. }
  28. public static string GetCurrentDesktopFolderPath()
  29. {
  30. return Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
  31. }
  32. public static ArrayList GetAll(string ExetensionName, string dir_path, bool deep_search, int search_depth)
  33. {
  34. if (!Directory.Exists(dir_path)) return null;
  35. DirectoryInfo dir = new DirectoryInfo(dir_path);
  36. ArrayList FileList = new ArrayList();
  37. FileInfo[] allFile = dir.GetFiles();
  38. foreach (FileInfo fi in allFile)
  39. {
  40. if (fi.Extension.ToUpper() == ExetensionName.ToUpper())
  41. FileList.Add(Path.Combine(dir.ToString() + "/", fi.Name));
  42. }
  43. if (deep_search && (search_depth > 0 || search_depth == -1))
  44. {
  45. DirectoryInfo[] allDir = dir.GetDirectories();
  46. foreach (DirectoryInfo d in allDir)
  47. {
  48. ArrayList cfl = GetAll(ExetensionName, d.ToString(), deep_search, (search_depth == -1) ? -1 : (search_depth - 1));
  49. if (cfl != null)
  50. {
  51. FileList.AddRange(cfl);
  52. }
  53. }
  54. }
  55. return FileList;
  56. }
  57. private static string GetTargetFileName(string Shortcurt_FilePath)
  58. {
  59. if (System.IO.File.Exists(Shortcurt_FilePath))
  60. {
  61. WshShell shell = new WshShell();
  62. IWshShortcut wst = (IWshShortcut)shell.CreateShortcut(Shortcurt_FilePath);
  63. //快捷方式文件指向的路径.Text = wst.TargetPath;
  64. //快捷方式文件指向的目标目录.Text = wst.WorkingDirectory;
  65. return wst.TargetPath;
  66. }
  67. else
  68. {
  69. return "";
  70. }
  71. }
  72. private static string GetUpdaterName(bool CheckExists)
  73. {
  74. string filename = Path.Combine(Application.StartupPath + @"\", "Mes.AutoUpdate.exe");
  75. if (CheckExists && !System.IO.File.Exists(filename))
  76. {
  77. return "";
  78. }
  79. return filename;
  80. }
  81. public static void Update_UPM()
  82. {
  83. string ServerFileVersion = "";
  84. string CurrentFileVersion = "0.0.0.0";
  85. string VerFile = Application.StartupPath + @"\" + "Mes.AutoUpdate.ver";
  86. bool NeedUpdate = false;
  87. string UpdateFileName = GetUpdaterName(true);
  88. if (string.IsNullOrEmpty(UpdateFileName))
  89. {
  90. NeedUpdate = true;
  91. UpdateFileName = GetUpdaterName(false);
  92. }
  93. else
  94. {
  95. try
  96. {
  97. FileVersionInfo fileVerInfo = System.Diagnostics.FileVersionInfo.GetVersionInfo(UpdateFileName);
  98. CurrentFileVersion = String.Format("{0}.{1}.{2}.{3}",
  99. fileVerInfo.FileMajorPart, fileVerInfo.FileMinorPart, fileVerInfo.FileBuildPart, fileVerInfo.FilePrivatePart);
  100. }
  101. catch { }
  102. if (!System.IO.File.Exists(Application.StartupPath + @"\" + "Mes.AutoUpdate.ver"))
  103. {
  104. ServerFileVersion = "999.999.999.999";
  105. }
  106. else
  107. {
  108. try
  109. {
  110. INIFile inifile = new INIFile(VerFile);
  111. ServerFileVersion = inifile.ReadValue("COMMON", "VERSION").Trim();
  112. }
  113. catch
  114. {
  115. ServerFileVersion = "999.999.999.999";
  116. }
  117. }
  118. if (CheckVersion(CurrentFileVersion, ServerFileVersion)) NeedUpdate = true;
  119. }
  120. if (NeedUpdate)
  121. {
  122. WebClient wc = new WebClient();
  123. try
  124. {
  125. string FileName = Path.GetFileName(UpdateFileName);
  126. ExeConfigurationFileMap cfgMap = new ExeConfigurationFileMap();
  127. cfgMap.ExeConfigFilename = UpdateFileName + ".CONFIG";
  128. Configuration cfg = ConfigurationManager.OpenMappedExeConfiguration(cfgMap, ConfigurationUserLevel.None);
  129. string url = cfg.AppSettings.Settings["downloadUrl"].Value.ToString();
  130. WebClient webClient = new WebClient();
  131. string sub_path = "";
  132. try
  133. {
  134. string str = cfg.AppSettings.Settings["downloadUrlfile"].Value.ToString();
  135. webClient.DownloadFile(url + str, Application.StartupPath + "\\~filelist.xml");
  136. DataSet uSet = new DataSet();
  137. uSet.ReadXml(Application.StartupPath + "\\~filelist.xml");
  138. sub_path = Convert.ToString(uSet.Tables["path"].Rows[0][0]);
  139. }
  140. catch
  141. {
  142. sub_path = "newfiles/";
  143. }
  144. finally
  145. {
  146. webClient.Dispose();
  147. System.IO.File.Delete(Application.StartupPath + "\\~filelist.xml");
  148. }
  149. wc.DownloadFile(url + @"/" + sub_path + Path.GetFileName(UpdateFileName), UpdateFileName + ".up");
  150. System.IO.File.Delete(UpdateFileName);
  151. System.IO.File.Move(UpdateFileName + ".up", UpdateFileName);
  152. wc.DownloadFile(url + @"/" + sub_path + Path.GetFileName(UpdateFileName) + ".config", UpdateFileName + ".config");
  153. if (!System.IO.File.Exists(Application.StartupPath + @"\" + "Mes.AutoUpdate.ver"))
  154. {
  155. try
  156. {
  157. FileVersionInfo fileVerInfo = System.Diagnostics.FileVersionInfo.GetVersionInfo(UpdateFileName);
  158. CurrentFileVersion = String.Format("{0}.{1}.{2}.{3}",
  159. fileVerInfo.FileMajorPart, fileVerInfo.FileMinorPart, fileVerInfo.FileBuildPart, fileVerInfo.FilePrivatePart);
  160. INIFile inifile = new INIFile(VerFile);
  161. inifile.WriteValue("COMMON", "VERSION", CurrentFileVersion);
  162. }
  163. catch
  164. {
  165. }
  166. }
  167. }
  168. catch (Exception ex) { Debug.Print(ex.Message); }
  169. finally
  170. {
  171. wc.Dispose();
  172. }
  173. }
  174. }
  175. private static bool CheckVersion(string CurrentVersion, string ServerVersion)
  176. {
  177. string[] CV = CurrentVersion.Trim().Split('.');
  178. string[] SV = ServerVersion.Trim().Split('.');
  179. for (int idx = 0; idx < 4; idx++)
  180. {
  181. int cvi = 0;
  182. int svi = 0;
  183. if (idx <= CV.GetLength(0) - 1)
  184. {
  185. int.TryParse(CV[idx], out cvi);
  186. }
  187. if (idx <= SV.GetLength(0) - 1)
  188. {
  189. int.TryParse(SV[idx], out svi);
  190. }
  191. if (cvi < svi) return true;
  192. }
  193. return false;
  194. }
  195. //更改桌面启动 MES主客户端 的快捷方式为启动 MES.UPDATE.
  196. public static void Update_Shortcut()
  197. {
  198. ArrayList lnks = GetAll(".LNK", GetAllUsersDesktopFolderPath(), true, 1);
  199. lnks.AddRange(GetAll(".LNK", GetCurrentDesktopFolderPath(), true, 1));
  200. string UpdateFileName = GetUpdaterName(true);
  201. string MESFileName = Path.Combine(Application.StartupPath + @"\", "Core.Mes.ClientManager.exe");
  202. if (string.IsNullOrEmpty(UpdateFileName)) return;
  203. bool existsUpdate = false;
  204. bool existsMes = false;
  205. string MesLnk = "";
  206. foreach (string lnk in lnks)
  207. {
  208. try
  209. {
  210. string tg = GetTargetFileName(lnk);
  211. if (Path.GetFullPath(tg).ToUpper() == Path.GetFullPath(MESFileName).ToUpper())
  212. {
  213. existsMes = true;
  214. MesLnk = lnk;
  215. }
  216. else if (Path.GetFullPath(tg).ToUpper() == Path.GetFullPath(UpdateFileName).ToUpper())
  217. {
  218. existsUpdate = true;
  219. }
  220. }
  221. catch { }
  222. }
  223. if (!existsUpdate && existsMes)
  224. {
  225. WshShell shell = new WshShell();
  226. IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(MesLnk);
  227. shortcut.TargetPath = Path.GetFullPath(UpdateFileName);
  228. shortcut.Save();
  229. }
  230. }
  231. }
  232. }