CheckUpdateWindow.cs 12 KB

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