CheckUpdateWindow.cs 11 KB

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