CheckUpdateWindow.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. using ICSharpCode.SharpZipLib.Zip;
  2. using System;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Net;
  6. using System.Windows.Forms;
  7. using System.Xml;
  8. namespace UAS_AutoUpdate
  9. {
  10. public partial class CheckUpdateWindow : Form
  11. {
  12. public static bool Zipped = false;
  13. string ConfigFile = "Config.xml";
  14. string ServerConfigFile = "ServerConfig.xml";
  15. public bool CloseWhenFinished = false;
  16. public CheckUpdateWindow()
  17. {
  18. InitializeComponent();
  19. StartPosition = FormStartPosition.CenterScreen;
  20. }
  21. private void CheckUpdateWindow_Load(object sender, EventArgs e)
  22. {
  23. try
  24. {
  25. FileInfo fileInfo = new FileInfo(ConfigFile);
  26. if (fileInfo.Length == 0)
  27. {
  28. XmlDocument xmlDocument = new XmlDocument();
  29. XmlNode newChild = xmlDocument.CreateXmlDeclaration("1.0", "utf-8", "");
  30. xmlDocument.AppendChild(newChild);
  31. XmlElement newChild2 = xmlDocument.CreateElement("cacheInfo");
  32. xmlDocument.AppendChild(newChild2);
  33. xmlDocument.Save(ConfigFile);
  34. }
  35. }
  36. catch (Exception)
  37. {
  38. MessageBox.Show("配置文件丢失,程序无法自动升级");
  39. Process process = Process.Start("UAS_MES_NEW.exe");
  40. CloseWhenFinished = true;
  41. Close();
  42. return;
  43. }
  44. try
  45. {
  46. WebClient webClient = new WebClient();
  47. webClient.DownloadFile(new Uri(GetCacheData(ConfigFile, "ServerConfigPath").ToString()), ServerConfigFile);
  48. Version version = new Version(GetCacheData(ServerConfigFile, "Version").ToString());
  49. Version v = new Version(GetCacheData(ConfigFile, "Version").ToString());
  50. if (version > v)
  51. {
  52. Process[] processesByName = Process.GetProcessesByName("UAS_MES_NEW");
  53. if (processesByName.Length == 0)
  54. {
  55. goto IL_01b0;
  56. }
  57. string a = MessageBox.Show(base.ParentForm, "检测到程序仍在运行,是否关闭", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  58. if (a == "Yes")
  59. {
  60. for (int i = 0; i < processesByName.Length; i++)
  61. {
  62. Process.GetProcessById(processesByName[i].Id).Kill();
  63. }
  64. goto IL_01b0;
  65. }
  66. CloseWhenFinished = true;
  67. Close();
  68. }
  69. else
  70. {
  71. Process process2 = Process.Start("UAS_MES_NEW.exe");
  72. CloseWhenFinished = true;
  73. Close();
  74. }
  75. goto end_IL_0095;
  76. IL_01b0:
  77. webClient.DownloadProgressChanged += Wc_DownloadProgressChanged;
  78. webClient.DownloadFileAsync(new Uri(GetCacheData(ConfigFile, "UpdatePath").ToString()), "UAS_MES.zip");
  79. SetCacheData(ConfigFile, "Version", version.ToString());
  80. end_IL_0095:;
  81. }
  82. catch (Exception ex2)
  83. {
  84. LogManager.DoLog(ex2.Message.ToString());
  85. Process process3 = Process.Start("UAS_MES_NEW.exe");
  86. CloseWhenFinished = true;
  87. Close();
  88. }
  89. }
  90. public static void SetCacheData(string FileName, string ParamName, object Value)
  91. {
  92. try
  93. {
  94. //根据地址读取xml文件
  95. XmlDocument doc = new XmlDocument();
  96. XmlReaderSettings settings = new XmlReaderSettings { CheckCharacters = false };
  97. //忽略文档里面的注释
  98. settings.IgnoreComments = true;
  99. XmlReader reader = XmlReader.Create(FileName, settings);
  100. doc.Load(reader);
  101. //先得到根节点
  102. XmlNode rootNode = doc.SelectSingleNode("cacheInfo");
  103. //再由根节点去找制定的节点
  104. XmlNodeList nodeList = rootNode.ChildNodes;
  105. bool flag = false;
  106. foreach (XmlNode node in nodeList)
  107. {
  108. //找到了这个节点名字
  109. if (node.Name == ParamName)
  110. {
  111. //就直接赋值
  112. node.InnerText = Value.ToString();
  113. flag = true;
  114. }
  115. }
  116. //如果没有该节点,就创建节点保存结果
  117. if (!flag)
  118. {
  119. //创建节点
  120. XmlElement newNode = doc.CreateElement(ParamName);
  121. XmlAttribute attr = doc.CreateAttribute("Type");
  122. attr.InnerText = Value.GetType().ToString();
  123. newNode.InnerText = Value.ToString();
  124. newNode.SetAttributeNode(attr);
  125. //讲新建的节点挂到根节点上
  126. rootNode.AppendChild(newNode);
  127. }
  128. //关闭Reader
  129. reader.Close();
  130. doc.Save(FileName);
  131. }
  132. catch (Exception)
  133. {
  134. }
  135. }
  136. public static object GetCacheData(string FileName, string ParamName)
  137. {
  138. try
  139. {
  140. object o = null;
  141. //根据地址读取xml文件
  142. XmlDocument doc = new XmlDocument();
  143. XmlReaderSettings settings = new XmlReaderSettings { CheckCharacters = false };
  144. //忽略文档里面的注释
  145. settings.IgnoreComments = true;
  146. XmlReader reader = XmlReader.Create(FileName, settings);
  147. doc.Load(reader);
  148. //先得到根节点
  149. XmlNode rootNode = doc.SelectSingleNode("cacheInfo");
  150. //再由根节点去找制定的节点
  151. XmlNodeList nodeList = rootNode.ChildNodes;
  152. foreach (XmlNode node in nodeList)
  153. {
  154. //找到了这个节点名字
  155. if (node.Name == ParamName)
  156. {
  157. //返回节点的内容
  158. switch (((XmlElement)node).GetAttribute("Type"))
  159. {
  160. case "System.String":
  161. o = node.InnerText;
  162. break;
  163. case "System.Int32":
  164. o = int.Parse(node.InnerText);
  165. break;
  166. case "System.Boolean":
  167. o = node.InnerText == "True" ? true : false;
  168. break;
  169. default:
  170. break;
  171. }
  172. break;
  173. }
  174. }
  175. //关闭reader
  176. reader.Close();
  177. if (o == null)
  178. return "";
  179. else
  180. return o;
  181. }
  182. catch (Exception)
  183. {
  184. return "";
  185. }
  186. }
  187. private void Wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
  188. {
  189. Action act = () =>
  190. {
  191. _progressbar.Value = e.ProgressPercentage;
  192. _processrate.Text = e.ProgressPercentage + "%";
  193. };
  194. if (IsHandleCreated)
  195. this.Invoke(act);
  196. if (e.ProgressPercentage == 100)
  197. {
  198. try
  199. {
  200. if (!Zipped)
  201. {
  202. ZipHelper.UnZip(Application.StartupPath + @"\UAS_MES.zip", Application.StartupPath);
  203. Process p = Process.Start("UAS_MES_NEW.exe");
  204. CloseWhenFinished = true;
  205. Close();
  206. }
  207. else
  208. {
  209. CloseWhenFinished = true;
  210. Close();
  211. }
  212. }
  213. catch (Exception ex)
  214. {
  215. MessageBox.Show(ex.Message);
  216. }
  217. }
  218. }
  219. private void CheckUpdateWindow_FormClosing(object sender, FormClosingEventArgs e)
  220. {
  221. if (!CloseWhenFinished)
  222. e.Cancel = true;
  223. }
  224. }
  225. public class ZipHelper
  226. {
  227. /// <summary>
  228. /// 用于解压缩Zip文件
  229. /// </summary>
  230. /// <param name="ZipFilePath"></param>
  231. /// <param name="UnZipPath"></param>
  232. public static void UnZip(string ZipFilePath, string UnZipPath)
  233. {
  234. try
  235. {
  236. if (!File.Exists(ZipFilePath))
  237. {
  238. throw new FileNotFoundException(string.Format("未能找到文件 '{0}' ", ZipFilePath));
  239. }
  240. if (!Directory.Exists(UnZipPath))
  241. {
  242. Directory.CreateDirectory(UnZipPath);
  243. }
  244. using (var s = new ZipInputStream(File.OpenRead(ZipFilePath)))
  245. {
  246. ZipEntry theEntry;
  247. while ((theEntry = s.GetNextEntry()) != null)
  248. {
  249. if (theEntry.IsDirectory)
  250. {
  251. continue;
  252. }
  253. string directorName = Path.Combine(UnZipPath, Path.GetDirectoryName(theEntry.Name));
  254. string fileName = Path.Combine(directorName, Path.GetFileName(theEntry.Name));
  255. if (!Directory.Exists(directorName))
  256. {
  257. Directory.CreateDirectory(directorName);
  258. }
  259. if (!string.IsNullOrEmpty(fileName))
  260. {
  261. try
  262. {
  263. //防止文件正在使用中报错
  264. using (FileStream streamWriter = File.Create(fileName))
  265. {
  266. int size = 4096;
  267. byte[] data = new byte[size];
  268. while (size > 0)
  269. {
  270. size = s.Read(data, 0, data.Length);
  271. streamWriter.Write(data, 0, size);
  272. }
  273. }
  274. }
  275. catch (Exception ex)
  276. {
  277. MessageBox.Show(ex.Message);
  278. }
  279. }
  280. }
  281. File.Delete(Application.StartupPath + @"\UAS_MES.zip");
  282. CheckUpdateWindow.Zipped = true;
  283. }
  284. }
  285. catch (Exception ex)
  286. {
  287. MessageBox.Show(ex.Message);
  288. }
  289. }
  290. }
  291. }