CheckUpdateWindow.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 CheckUpdateWindow()
  16. {
  17. InitializeComponent();
  18. StartPosition = FormStartPosition.CenterScreen;
  19. }
  20. private void CheckUpdateWindow_Load(object sender, EventArgs e)
  21. {
  22. //使用WebClient从指定位置下载文件,然后进行解压缩覆盖
  23. FileInfo info = new FileInfo(ConfigFile);
  24. if (info.Length == 0)
  25. {
  26. XmlDocument doc = new XmlDocument();
  27. //创建类型声明节点
  28. XmlNode node = doc.CreateXmlDeclaration("1.0", "utf-8", "");
  29. doc.AppendChild(node);
  30. //创建根节点
  31. XmlElement xeRoot = doc.CreateElement("cacheInfo");
  32. doc.AppendChild(xeRoot);
  33. doc.Save(ConfigFile);
  34. }
  35. WebClient wc = new WebClient();
  36. wc.DownloadFile(new Uri(GetCacheData(ConfigFile, "ServerConfigPath").ToString()), ServerConfigFile);
  37. //服务器获取的配置文件
  38. Version ver1 = new Version(GetCacheData(ServerConfigFile, "Version").ToString());
  39. //本地的配置文件
  40. Version ver2 = new Version(GetCacheData(ConfigFile, "Version").ToString());
  41. //进行版本的比较
  42. if (ver1 > ver2)
  43. {
  44. Process[] pro = Process.GetProcessesByName("UAS_MES");
  45. if (pro.Length > 0)
  46. {
  47. string CloseProcess = MessageBox.Show(this.ParentForm, "当前程序仍在运行,是否关闭", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  48. if (CloseProcess == "Yes")
  49. {
  50. for (int i = 0; i < pro.Length; i++)
  51. {
  52. Process.GetProcessById(pro[i].Id).Kill();
  53. }
  54. }
  55. else
  56. {
  57. return;
  58. }
  59. }
  60. wc.DownloadProgressChanged += Wc_DownloadProgressChanged;
  61. wc.DownloadFileAsync(new Uri(GetCacheData(ConfigFile, "UpdatePath").ToString()), "UAS_MES.zip");
  62. SetCacheData(ConfigFile, "Version", ver1.ToString());
  63. }
  64. else
  65. {
  66. Process p = Process.Start("UAS_MES.exe");
  67. Close();
  68. }
  69. }
  70. public static void SetCacheData(string FileName, string ParamName, object Value)
  71. {
  72. try
  73. {
  74. //根据地址读取xml文件
  75. XmlDocument doc = new XmlDocument();
  76. XmlReaderSettings settings = new XmlReaderSettings { CheckCharacters = false };
  77. //忽略文档里面的注释
  78. settings.IgnoreComments = true;
  79. XmlReader reader = XmlReader.Create(FileName, settings);
  80. doc.Load(reader);
  81. //先得到根节点
  82. XmlNode rootNode = doc.SelectSingleNode("cacheInfo");
  83. //再由根节点去找制定的节点
  84. XmlNodeList nodeList = rootNode.ChildNodes;
  85. bool flag = false;
  86. foreach (XmlNode node in nodeList)
  87. {
  88. //找到了这个节点名字
  89. if (node.Name == ParamName)
  90. {
  91. //就直接赋值
  92. node.InnerText = Value.ToString();
  93. flag = true;
  94. }
  95. }
  96. //如果没有该节点,就创建节点保存结果
  97. if (!flag)
  98. {
  99. //创建节点
  100. XmlElement newNode = doc.CreateElement(ParamName);
  101. XmlAttribute attr = doc.CreateAttribute("Type");
  102. attr.InnerText = Value.GetType().ToString();
  103. newNode.InnerText = Value.ToString();
  104. newNode.SetAttributeNode(attr);
  105. //讲新建的节点挂到根节点上
  106. rootNode.AppendChild(newNode);
  107. }
  108. //关闭Reader
  109. reader.Close();
  110. doc.Save(FileName);
  111. }
  112. catch (Exception)
  113. {
  114. }
  115. }
  116. public static object GetCacheData(string FileName, string ParamName)
  117. {
  118. try
  119. {
  120. object o = null;
  121. //根据地址读取xml文件
  122. XmlDocument doc = new XmlDocument();
  123. XmlReaderSettings settings = new XmlReaderSettings { CheckCharacters = false };
  124. //忽略文档里面的注释
  125. settings.IgnoreComments = true;
  126. XmlReader reader = XmlReader.Create(FileName, settings);
  127. doc.Load(reader);
  128. //先得到根节点
  129. XmlNode rootNode = doc.SelectSingleNode("cacheInfo");
  130. //再由根节点去找制定的节点
  131. XmlNodeList nodeList = rootNode.ChildNodes;
  132. foreach (XmlNode node in nodeList)
  133. {
  134. //找到了这个节点名字
  135. if (node.Name == ParamName)
  136. {
  137. //返回节点的内容
  138. switch (((XmlElement)node).GetAttribute("Type"))
  139. {
  140. case "System.String":
  141. o = node.InnerText;
  142. break;
  143. case "System.Int32":
  144. o = int.Parse(node.InnerText);
  145. break;
  146. case "System.Boolean":
  147. o = node.InnerText == "True" ? true : false;
  148. break;
  149. default:
  150. break;
  151. }
  152. break;
  153. }
  154. }
  155. //关闭reader
  156. reader.Close();
  157. if (o == null)
  158. return "";
  159. else
  160. return o;
  161. }
  162. catch (Exception)
  163. {
  164. return "";
  165. }
  166. }
  167. private void Wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
  168. {
  169. Action act = () =>
  170. {
  171. _progressbar.Value = e.ProgressPercentage;
  172. _processrate.Text = e.ProgressPercentage + "%";
  173. };
  174. if (IsHandleCreated)
  175. this.Invoke(act);
  176. if (e.ProgressPercentage == 100)
  177. {
  178. if (!Zipped)
  179. {
  180. ZipHelper.UnZip(Application.StartupPath + @"\UAS_MES.zip", Application.StartupPath);
  181. }
  182. else
  183. {
  184. Close();
  185. }
  186. }
  187. }
  188. }
  189. public class ZipHelper
  190. {
  191. /// <summary>
  192. /// 用于解压缩Zip文件
  193. /// </summary>
  194. /// <param name="ZipFilePath"></param>
  195. /// <param name="UnZipPath"></param>
  196. public static void UnZip(string ZipFilePath, string UnZipPath)
  197. {
  198. if (!File.Exists(ZipFilePath))
  199. {
  200. throw new FileNotFoundException(string.Format("未能找到文件 '{0}' ", ZipFilePath));
  201. }
  202. if (!Directory.Exists(UnZipPath))
  203. {
  204. Directory.CreateDirectory(UnZipPath);
  205. }
  206. using (var s = new ZipInputStream(File.OpenRead(ZipFilePath)))
  207. {
  208. ZipEntry theEntry;
  209. while ((theEntry = s.GetNextEntry()) != null)
  210. {
  211. if (theEntry.IsDirectory)
  212. {
  213. continue;
  214. }
  215. string directorName = Path.Combine(UnZipPath, Path.GetDirectoryName(theEntry.Name));
  216. string fileName = Path.Combine(directorName, Path.GetFileName(theEntry.Name));
  217. if (!Directory.Exists(directorName))
  218. {
  219. Directory.CreateDirectory(directorName);
  220. }
  221. if (!string.IsNullOrEmpty(fileName))
  222. {
  223. try
  224. {
  225. //防止文件正在使用中报错
  226. using (FileStream streamWriter = File.Create(fileName))
  227. {
  228. int size = 4096;
  229. byte[] data = new byte[size];
  230. while (size > 0)
  231. {
  232. size = s.Read(data, 0, data.Length);
  233. streamWriter.Write(data, 0, size);
  234. }
  235. }
  236. }
  237. catch (Exception)
  238. {
  239. }
  240. }
  241. }
  242. File.Delete(Application.StartupPath + @"\UAS_MES.zip");
  243. CheckUpdateWindow.Zipped = true;
  244. Process p = Process.Start("UAS_MES.exe");
  245. }
  246. }
  247. }
  248. }