MainForm.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Diagnostics;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Net;
  8. using System.Threading;
  9. using System.Windows.Forms;
  10. using System.Xml.Linq;
  11. namespace prBrowserUpdate
  12. {
  13. public class MainForm : Form
  14. {
  15. public string currentPath = "";
  16. public string serverPath = "";
  17. public string serverFTPName = "RKFTP";
  18. public string serverFTPPassword = "pryk@2020";
  19. public string ftpDefaultFolder = "browser";
  20. private IContainer components;
  21. private Label label3;
  22. private Button BtnUpdate;
  23. private Button BtnCancel;
  24. private Label labelUpdateFile;
  25. public MainForm()
  26. {
  27. InitializeComponent();
  28. }
  29. private void MainForm_Load(object sender, EventArgs e)
  30. {
  31. getPath();
  32. }
  33. private void getPath()
  34. {
  35. currentPath = AppDomain.CurrentDomain.BaseDirectory;
  36. serverPath = getXMLValue("prBrowser.exe.config", "UpdateUrl");
  37. }
  38. private string getVersion(string path)
  39. {
  40. string version = "";
  41. try
  42. {
  43. FileVersionInfo file = FileVersionInfo.GetVersionInfo(path);
  44. return $"{file.FileMajorPart}.{file.FileMinorPart}.{file.FileBuildPart}.{file.FilePrivatePart}";
  45. }
  46. catch
  47. {
  48. return "";
  49. }
  50. }
  51. private string getXMLValue(string path, string attr)
  52. {
  53. string xmlValue = "";
  54. foreach (XElement item in XDocument.Load(path).Root.Element("appSettings").Elements())
  55. {
  56. if (item.Attribute("key").Value == attr)
  57. {
  58. xmlValue = item.Attribute("value").Value;
  59. }
  60. }
  61. return xmlValue;
  62. }
  63. private void BtnCancel_Click(object sender, EventArgs e)
  64. {
  65. this.Close();
  66. //Application.Exit();
  67. }
  68. private void BtnUpdate_Click(object sender, EventArgs e)
  69. {
  70. UpdateApp();
  71. BtnUpdate.Enabled = false;
  72. }
  73. /// <summary>
  74. /// 更新应用程序
  75. /// </summary>
  76. private void UpdateApp()
  77. {
  78. if (getUpdateList() != "0")
  79. {
  80. return;
  81. }
  82. DirectoryInfo directoryInfo = new DirectoryInfo(currentPath + "UpdateList\\");
  83. string latestFileName = "";
  84. FileInfo[] files = directoryInfo.GetFiles();
  85. for (int i = 0; i < files.Length; i++)
  86. {
  87. latestFileName = files[i].FullName;
  88. }
  89. if (latestFileName == "")
  90. {
  91. return;
  92. }
  93. IEnumerable<XElement> enumerable = XDocument.Load(latestFileName).Root.Element("list").Elements();
  94. string message = "";
  95. foreach (XElement item in enumerable)
  96. {
  97. string itemKey = item.Attribute("key").Value;
  98. string itemValue = item.Attribute("value").Value;
  99. string serverItemValue = itemValue;
  100. serverItemValue = ((!(serverItemValue == "")) ? ("/" + serverItemValue) : "/");
  101. string localPath = currentPath + itemValue;
  102. string uri = serverPath + ftpDefaultFolder + serverItemValue.Replace("\\", "/");
  103. message = DownLoad(localPath, itemKey, uri, serverFTPName, serverFTPPassword);
  104. if (message == "0")
  105. {
  106. setUpdateFile(currentPath + itemValue + itemKey);
  107. continue;
  108. }
  109. break;
  110. }
  111. if (message == "0")
  112. {
  113. MessageBox.Show("更新完成");
  114. //完全退出,再次进入 QY
  115. System.Environment.Exit(0);
  116. ExecuteAsAdmin("prBrowser.exe");
  117. //Environment.Exit(0);
  118. }
  119. else
  120. {
  121. MessageBox.Show(message);
  122. }
  123. }
  124. /// <summary>
  125. /// 获取更新列表
  126. /// </summary>
  127. /// <returns></returns>
  128. private string getUpdateList()
  129. {
  130. string message = "0";
  131. try
  132. {
  133. string uri = serverPath + ftpDefaultFolder + "/UpdateList/";
  134. FtpWebRequest obj = (FtpWebRequest)WebRequest.Create(new Uri(uri));
  135. obj.Credentials = new NetworkCredential(serverFTPName, serverFTPPassword);
  136. obj.Method = "LIST";
  137. WebResponse response = obj.GetResponse();
  138. StreamReader reader = new StreamReader(response.GetResponseStream());
  139. for (string line = reader.ReadLine(); line != null; line = reader.ReadLine())
  140. {
  141. if (!line.Contains("<DIR>"))
  142. {
  143. string msg = line.Substring(39).Trim();
  144. string rtn = DownLoad(currentPath + "UpdateList\\", msg, uri, serverFTPName, serverFTPPassword);
  145. if (rtn != "0")
  146. {
  147. message = rtn;
  148. break;
  149. }
  150. }
  151. }
  152. reader.Close();
  153. response.Close();
  154. return message;
  155. }
  156. catch (Exception ex)
  157. {
  158. return ex.Message;
  159. }
  160. }
  161. private void delUpdateList()
  162. {
  163. new DirectoryInfo(currentPath + "UpdateList\\").Delete(recursive: true);
  164. }
  165. public void setUpdateFile(string fileName)
  166. {
  167. labelUpdateFile.Text = fileName;
  168. labelUpdateFile.Refresh();
  169. label3.Refresh();
  170. BtnUpdate.Refresh();
  171. BtnCancel.Refresh();
  172. }
  173. /// <summary>
  174. /// 下载方法
  175. /// </summary>
  176. /// <param name="filePath">文件地址</param>
  177. /// <param name="fileName">文件名</param>
  178. /// <param name="ftpPath">网络FTP地址</param>
  179. /// <param name="ftpUserName">用户名</param>
  180. /// <param name="ftpPassWord">密码</param>
  181. /// <returns></returns>
  182. private string DownLoad(string filePath, string fileName, string ftpPath, string ftpUserName, string ftpPassWord)
  183. {
  184. try
  185. {
  186. if (!Directory.Exists(filePath))
  187. {
  188. Directory.CreateDirectory(filePath);
  189. }
  190. FileStream outputStream = new FileStream(filePath + fileName, FileMode.Create);
  191. FtpWebRequest obj = (FtpWebRequest)WebRequest.Create(new Uri(ftpPath + fileName));
  192. obj.Method = "RETR";
  193. obj.Credentials = new NetworkCredential(ftpUserName, ftpPassWord);
  194. obj.UseBinary = true;
  195. FtpWebResponse response = (FtpWebResponse)obj.GetResponse();
  196. Stream ftpStream = response.GetResponseStream();
  197. _ = response.ContentLength;
  198. int bufferSize = 2048;
  199. byte[] buffer = new byte[bufferSize];
  200. for (int readCount = ftpStream.Read(buffer, 0, bufferSize); readCount > 0; readCount = ftpStream.Read(buffer, 0, bufferSize))
  201. {
  202. outputStream.Write(buffer, 0, readCount);
  203. }
  204. ftpStream.Close();
  205. outputStream.Close();
  206. response.Close();
  207. return "0";
  208. }
  209. catch (Exception ex)
  210. {
  211. return ex.Message;
  212. }
  213. }
  214. public string FTPDownLoad(string filePath, string fileName, string ftpPath, string RequedstPath, string ftpUserName, string ftpPassWord)
  215. {
  216. try
  217. {
  218. string uri = ftpPath + RequedstPath;
  219. FtpWebRequest obj = (FtpWebRequest)WebRequest.Create(new Uri(uri));
  220. obj.Credentials = new NetworkCredential(ftpUserName, ftpPassWord);
  221. obj.Method = "LIST";
  222. WebResponse response = obj.GetResponse();
  223. StreamReader reader = new StreamReader(response.GetResponseStream());
  224. string line = reader.ReadLine();
  225. while (line != null)
  226. {
  227. if (line.Contains("web.config") || line.Contains("debug.log") || line.Contains("prBrowser.exe.config"))
  228. {
  229. line = reader.ReadLine();
  230. continue;
  231. }
  232. if (!line.Contains("<DIR>"))
  233. {
  234. string msg = line.Substring(39).Trim();
  235. setUpdateFile(filePath + msg);
  236. if (!(DownLoad(filePath, msg, uri + "/", ftpUserName, ftpPassWord) == "0"))
  237. {
  238. }
  239. }
  240. else
  241. {
  242. string msg2 = line.Substring(line.LastIndexOf("<DIR>") + 5).Trim();
  243. if (msg2 != "log")
  244. {
  245. FTPDownLoad(filePath + msg2 + "/", "", uri + "/", msg2, ftpUserName, ftpPassWord);
  246. }
  247. }
  248. line = reader.ReadLine();
  249. }
  250. reader.Close();
  251. response.Close();
  252. return "0";
  253. }
  254. catch (Exception ex)
  255. {
  256. return ex.Message;
  257. }
  258. }
  259. public void ExecuteAsAdmin(string fileName)
  260. {
  261. Process process = new Process();
  262. process.StartInfo.FileName = fileName;
  263. process.StartInfo.UseShellExecute = true;
  264. process.StartInfo.Verb = "runas";
  265. process.Start();
  266. }
  267. private void BtnUpdateDelay(object sender, EventArgs e)
  268. {
  269. Thread.Sleep(1000);
  270. UpdateApp();
  271. BtnUpdate.Enabled = false;
  272. }
  273. protected override void Dispose(bool disposing)
  274. {
  275. if (disposing && components != null)
  276. {
  277. components.Dispose();
  278. }
  279. base.Dispose(disposing);
  280. }
  281. private void InitializeComponent()
  282. {
  283. System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(prBrowserUpdate.MainForm));
  284. label3 = new System.Windows.Forms.Label();
  285. BtnUpdate = new System.Windows.Forms.Button();
  286. BtnCancel = new System.Windows.Forms.Button();
  287. labelUpdateFile = new System.Windows.Forms.Label();
  288. SuspendLayout();
  289. label3.AutoSize = true;
  290. label3.Location = new System.Drawing.Point(33, 50);
  291. label3.Name = "label3";
  292. label3.Size = new System.Drawing.Size(77, 12);
  293. label3.TabIndex = 2;
  294. label3.Text = "更新中......";
  295. BtnUpdate.Location = new System.Drawing.Point(104, 163);
  296. BtnUpdate.Name = "BtnUpdate";
  297. BtnUpdate.Size = new System.Drawing.Size(75, 23);
  298. BtnUpdate.TabIndex = 5;
  299. BtnUpdate.Text = "更新";
  300. BtnUpdate.UseVisualStyleBackColor = true;
  301. BtnUpdate.Click += new System.EventHandler(BtnUpdate_Click);
  302. BtnCancel.Location = new System.Drawing.Point(283, 163);
  303. BtnCancel.Name = "BtnCancel";
  304. BtnCancel.Size = new System.Drawing.Size(75, 23);
  305. BtnCancel.TabIndex = 6;
  306. BtnCancel.Text = "退出";
  307. BtnCancel.UseVisualStyleBackColor = true;
  308. BtnCancel.Click += new System.EventHandler(BtnCancel_Click);
  309. labelUpdateFile.Location = new System.Drawing.Point(33, 72);
  310. labelUpdateFile.Name = "labelUpdateFile";
  311. labelUpdateFile.Size = new System.Drawing.Size(408, 65);
  312. labelUpdateFile.TabIndex = 7;
  313. base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 12f);
  314. base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  315. base.ClientSize = new System.Drawing.Size(478, 243);
  316. base.Controls.Add(labelUpdateFile);
  317. base.Controls.Add(BtnCancel);
  318. base.Controls.Add(BtnUpdate);
  319. base.Controls.Add(label3);
  320. base.Icon = (System.Drawing.Icon)resources.GetObject("$this.Icon");
  321. base.Name = "MainForm";
  322. base.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
  323. Text = "更新";
  324. base.Load += new System.EventHandler(MainForm_Load);
  325. base.Shown += new System.EventHandler(BtnUpdateDelay);
  326. ResumeLayout(false);
  327. PerformLayout();
  328. }
  329. }
  330. }