CheckUpdateWindow.cs 11 KB

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