CheckUpdateWindow.cs 11 KB

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