CheckUpdateWindow.cs 10 KB

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