CheckUpdateWindow.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. Close();
  58. return;
  59. }
  60. }
  61. wc.DownloadProgressChanged += Wc_DownloadProgressChanged;
  62. wc.DownloadFileAsync(new Uri(GetCacheData(ConfigFile, "UpdatePath").ToString()), "UAS_MES.zip");
  63. SetCacheData(ConfigFile, "Version", ver1.ToString());
  64. }
  65. else
  66. {
  67. Process p = Process.Start("UAS_MES.exe");
  68. Close();
  69. }
  70. }
  71. public static void SetCacheData(string FileName, string ParamName, object Value)
  72. {
  73. try
  74. {
  75. //根据地址读取xml文件
  76. XmlDocument doc = new XmlDocument();
  77. XmlReaderSettings settings = new XmlReaderSettings { CheckCharacters = false };
  78. //忽略文档里面的注释
  79. settings.IgnoreComments = true;
  80. XmlReader reader = XmlReader.Create(FileName, settings);
  81. doc.Load(reader);
  82. //先得到根节点
  83. XmlNode rootNode = doc.SelectSingleNode("cacheInfo");
  84. //再由根节点去找制定的节点
  85. XmlNodeList nodeList = rootNode.ChildNodes;
  86. bool flag = false;
  87. foreach (XmlNode node in nodeList)
  88. {
  89. //找到了这个节点名字
  90. if (node.Name == ParamName)
  91. {
  92. //就直接赋值
  93. node.InnerText = Value.ToString();
  94. flag = true;
  95. }
  96. }
  97. //如果没有该节点,就创建节点保存结果
  98. if (!flag)
  99. {
  100. //创建节点
  101. XmlElement newNode = doc.CreateElement(ParamName);
  102. XmlAttribute attr = doc.CreateAttribute("Type");
  103. attr.InnerText = Value.GetType().ToString();
  104. newNode.InnerText = Value.ToString();
  105. newNode.SetAttributeNode(attr);
  106. //讲新建的节点挂到根节点上
  107. rootNode.AppendChild(newNode);
  108. }
  109. //关闭Reader
  110. reader.Close();
  111. doc.Save(FileName);
  112. }
  113. catch (Exception)
  114. {
  115. }
  116. }
  117. public static object GetCacheData(string FileName, string ParamName)
  118. {
  119. try
  120. {
  121. object o = null;
  122. //根据地址读取xml文件
  123. XmlDocument doc = new XmlDocument();
  124. XmlReaderSettings settings = new XmlReaderSettings { CheckCharacters = false };
  125. //忽略文档里面的注释
  126. settings.IgnoreComments = true;
  127. XmlReader reader = XmlReader.Create(FileName, settings);
  128. doc.Load(reader);
  129. //先得到根节点
  130. XmlNode rootNode = doc.SelectSingleNode("cacheInfo");
  131. //再由根节点去找制定的节点
  132. XmlNodeList nodeList = rootNode.ChildNodes;
  133. foreach (XmlNode node in nodeList)
  134. {
  135. //找到了这个节点名字
  136. if (node.Name == ParamName)
  137. {
  138. //返回节点的内容
  139. switch (((XmlElement)node).GetAttribute("Type"))
  140. {
  141. case "System.String":
  142. o = node.InnerText;
  143. break;
  144. case "System.Int32":
  145. o = int.Parse(node.InnerText);
  146. break;
  147. case "System.Boolean":
  148. o = node.InnerText == "True" ? true : false;
  149. break;
  150. default:
  151. break;
  152. }
  153. break;
  154. }
  155. }
  156. //关闭reader
  157. reader.Close();
  158. if (o == null)
  159. return "";
  160. else
  161. return o;
  162. }
  163. catch (Exception)
  164. {
  165. return "";
  166. }
  167. }
  168. private void Wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
  169. {
  170. Action act = () =>
  171. {
  172. _progressbar.Value = e.ProgressPercentage;
  173. _processrate.Text = e.ProgressPercentage + "%";
  174. };
  175. if (IsHandleCreated)
  176. this.Invoke(act);
  177. if (e.ProgressPercentage == 100)
  178. {
  179. if (!Zipped)
  180. {
  181. ZipHelper.UnZip(Application.StartupPath + @"\UAS_MES.zip", Application.StartupPath);
  182. }
  183. else
  184. {
  185. Close();
  186. }
  187. }
  188. }
  189. }
  190. public class ZipHelper
  191. {
  192. /// <summary>
  193. /// 用于解压缩Zip文件
  194. /// </summary>
  195. /// <param name="ZipFilePath"></param>
  196. /// <param name="UnZipPath"></param>
  197. public static void UnZip(string ZipFilePath, string UnZipPath)
  198. {
  199. if (!File.Exists(ZipFilePath))
  200. {
  201. throw new FileNotFoundException(string.Format("未能找到文件 '{0}' ", ZipFilePath));
  202. }
  203. if (!Directory.Exists(UnZipPath))
  204. {
  205. Directory.CreateDirectory(UnZipPath);
  206. }
  207. using (var s = new ZipInputStream(File.OpenRead(ZipFilePath)))
  208. {
  209. ZipEntry theEntry;
  210. while ((theEntry = s.GetNextEntry()) != null)
  211. {
  212. if (theEntry.IsDirectory)
  213. {
  214. continue;
  215. }
  216. string directorName = Path.Combine(UnZipPath, Path.GetDirectoryName(theEntry.Name));
  217. string fileName = Path.Combine(directorName, Path.GetFileName(theEntry.Name));
  218. if (!Directory.Exists(directorName))
  219. {
  220. Directory.CreateDirectory(directorName);
  221. }
  222. if (!string.IsNullOrEmpty(fileName))
  223. {
  224. try
  225. {
  226. //防止文件正在使用中报错
  227. using (FileStream streamWriter = File.Create(fileName))
  228. {
  229. int size = 4096;
  230. byte[] data = new byte[size];
  231. while (size > 0)
  232. {
  233. size = s.Read(data, 0, data.Length);
  234. streamWriter.Write(data, 0, size);
  235. }
  236. }
  237. }
  238. catch (Exception)
  239. {
  240. }
  241. }
  242. }
  243. File.Delete(Application.StartupPath + @"\UAS_MES.zip");
  244. CheckUpdateWindow.Zipped = true;
  245. Process p = Process.Start("UAS_MES.exe");
  246. }
  247. }
  248. }
  249. }