CheckUpdateWindow.cs 10 KB

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