FrmUpdate.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using System.Xml;
  8. using System.Net;
  9. using System.IO;
  10. using System.Threading;
  11. using System.Diagnostics;
  12. //源码下载www.51aspx.com
  13. namespace AutoUpdate
  14. {
  15. /// <summary>
  16. /// Form1 的摘要说明。
  17. /// </summary>
  18. public class FrmUpdate : System.Windows.Forms.Form
  19. {
  20. private System.Windows.Forms.PictureBox pictureBox1;
  21. private System.Windows.Forms.Panel panel1;
  22. private System.Windows.Forms.GroupBox groupBox1;
  23. private System.Windows.Forms.ColumnHeader chFileName;
  24. private System.Windows.Forms.ColumnHeader chVersion;
  25. private System.Windows.Forms.ColumnHeader chProgress;
  26. private System.Windows.Forms.GroupBox groupBox2;
  27. private System.Windows.Forms.ListView lvUpdateList;
  28. private System.Windows.Forms.Label label1;
  29. private System.Windows.Forms.ProgressBar pbDownFile;
  30. private System.Windows.Forms.Panel panel2;
  31. private System.Windows.Forms.Label label4;
  32. private System.Windows.Forms.LinkLabel linkLabel1;
  33. private System.Windows.Forms.Label label3;
  34. private System.Windows.Forms.Label label2;
  35. private System.Windows.Forms.Label label5;
  36. private System.Windows.Forms.GroupBox groupBox3;
  37. private System.Windows.Forms.GroupBox groupBox4;
  38. private ListView lvUpdateList1;
  39. private ToolTip tip;
  40. private Button btnNext;
  41. private Button btnCancel;
  42. private Button btnFinish;
  43. private Button btnClose;
  44. private Label label6;
  45. private IContainer components;
  46. public FrmUpdate()
  47. {
  48. //
  49. // Windows 窗体设计器支持所必需的
  50. //
  51. InitializeComponent();
  52. CheckForIllegalCrossThreadCalls = false;
  53. AeroForm.AeroEffect(this);
  54. //
  55. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  56. //
  57. }
  58. /// <summary>
  59. /// 清理所有正在使用的资源。
  60. /// </summary>
  61. protected override void Dispose( bool disposing )
  62. {
  63. if( disposing )
  64. {
  65. if (components != null)
  66. {
  67. components.Dispose();
  68. }
  69. }
  70. base.Dispose( disposing );
  71. }
  72. #region Windows 窗体设计器生成的代码
  73. /// <summary>
  74. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  75. /// 此方法的内容。
  76. /// </summary>
  77. private void InitializeComponent()
  78. {
  79. this.components = new System.ComponentModel.Container();
  80. System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FrmUpdate));
  81. this.pictureBox1 = new System.Windows.Forms.PictureBox();
  82. this.panel1 = new System.Windows.Forms.Panel();
  83. this.lvUpdateList1 = new System.Windows.Forms.ListView();
  84. this.label1 = new System.Windows.Forms.Label();
  85. this.groupBox2 = new System.Windows.Forms.GroupBox();
  86. this.lvUpdateList = new System.Windows.Forms.ListView();
  87. this.chFileName = new System.Windows.Forms.ColumnHeader();
  88. this.chVersion = new System.Windows.Forms.ColumnHeader();
  89. this.chProgress = new System.Windows.Forms.ColumnHeader();
  90. this.pbDownFile = new System.Windows.Forms.ProgressBar();
  91. this.groupBox1 = new System.Windows.Forms.GroupBox();
  92. this.panel2 = new System.Windows.Forms.Panel();
  93. this.label6 = new System.Windows.Forms.Label();
  94. this.label5 = new System.Windows.Forms.Label();
  95. this.label4 = new System.Windows.Forms.Label();
  96. this.linkLabel1 = new System.Windows.Forms.LinkLabel();
  97. this.label3 = new System.Windows.Forms.Label();
  98. this.label2 = new System.Windows.Forms.Label();
  99. this.groupBox3 = new System.Windows.Forms.GroupBox();
  100. this.groupBox4 = new System.Windows.Forms.GroupBox();
  101. this.tip = new System.Windows.Forms.ToolTip(this.components);
  102. this.btnNext = new System.Windows.Forms.Button();
  103. this.btnCancel = new System.Windows.Forms.Button();
  104. this.btnFinish = new System.Windows.Forms.Button();
  105. this.btnClose = new System.Windows.Forms.Button();
  106. ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
  107. this.panel1.SuspendLayout();
  108. this.panel2.SuspendLayout();
  109. this.SuspendLayout();
  110. //
  111. // pictureBox1
  112. //
  113. this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
  114. this.pictureBox1.Location = new System.Drawing.Point(8, 8);
  115. this.pictureBox1.Name = "pictureBox1";
  116. this.pictureBox1.Size = new System.Drawing.Size(96, 240);
  117. this.pictureBox1.TabIndex = 1;
  118. this.pictureBox1.TabStop = false;
  119. //
  120. // panel1
  121. //
  122. this.panel1.Controls.Add(this.lvUpdateList1);
  123. this.panel1.Controls.Add(this.label1);
  124. this.panel1.Controls.Add(this.groupBox2);
  125. this.panel1.Controls.Add(this.lvUpdateList);
  126. this.panel1.Controls.Add(this.pbDownFile);
  127. this.panel1.Controls.Add(this.groupBox1);
  128. this.panel1.Location = new System.Drawing.Point(120, 8);
  129. this.panel1.Name = "panel1";
  130. this.panel1.Size = new System.Drawing.Size(381, 240);
  131. this.panel1.TabIndex = 2;
  132. //
  133. // lvUpdateList1
  134. //
  135. this.lvUpdateList1.Location = new System.Drawing.Point(260, 174);
  136. this.lvUpdateList1.Name = "lvUpdateList1";
  137. this.lvUpdateList1.Size = new System.Drawing.Size(121, 20);
  138. this.lvUpdateList1.TabIndex = 10;
  139. this.lvUpdateList1.UseCompatibleStateImageBehavior = false;
  140. this.lvUpdateList1.Visible = false;
  141. //
  142. // label1
  143. //
  144. this.label1.Location = new System.Drawing.Point(2, 16);
  145. this.label1.Name = "label1";
  146. this.label1.Size = new System.Drawing.Size(315, 16);
  147. this.label1.TabIndex = 9;
  148. this.label1.Text = "正在检查更新,请稍候...";
  149. //
  150. // groupBox2
  151. //
  152. this.groupBox2.Dock = System.Windows.Forms.DockStyle.Bottom;
  153. this.groupBox2.Location = new System.Drawing.Point(0, 232);
  154. this.groupBox2.Name = "groupBox2";
  155. this.groupBox2.Size = new System.Drawing.Size(381, 8);
  156. this.groupBox2.TabIndex = 7;
  157. this.groupBox2.TabStop = false;
  158. //
  159. // lvUpdateList
  160. //
  161. this.lvUpdateList.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
  162. this.chFileName,
  163. this.chVersion,
  164. this.chProgress});
  165. this.lvUpdateList.FullRowSelect = true;
  166. this.lvUpdateList.Location = new System.Drawing.Point(3, 48);
  167. this.lvUpdateList.Name = "lvUpdateList";
  168. this.lvUpdateList.Size = new System.Drawing.Size(375, 146);
  169. this.lvUpdateList.TabIndex = 6;
  170. this.lvUpdateList.UseCompatibleStateImageBehavior = false;
  171. this.lvUpdateList.View = System.Windows.Forms.View.Details;
  172. this.lvUpdateList.MouseMove += new System.Windows.Forms.MouseEventHandler(this.lvUpdateList_MouseMove);
  173. this.lvUpdateList.MouseLeave += new System.EventHandler(this.lvUpdateList_MouseLeave);
  174. //
  175. // chFileName
  176. //
  177. this.chFileName.Text = "组件名";
  178. this.chFileName.Width = 123;
  179. //
  180. // chVersion
  181. //
  182. this.chVersion.Text = "版本号";
  183. this.chVersion.Width = 106;
  184. //
  185. // chProgress
  186. //
  187. this.chProgress.Text = "进度";
  188. this.chProgress.Width = 116;
  189. //
  190. // pbDownFile
  191. //
  192. this.pbDownFile.Location = new System.Drawing.Point(3, 200);
  193. this.pbDownFile.Name = "pbDownFile";
  194. this.pbDownFile.Size = new System.Drawing.Size(375, 17);
  195. this.pbDownFile.TabIndex = 5;
  196. //
  197. // groupBox1
  198. //
  199. this.groupBox1.Location = new System.Drawing.Point(0, 29);
  200. this.groupBox1.Name = "groupBox1";
  201. this.groupBox1.Size = new System.Drawing.Size(381, 8);
  202. this.groupBox1.TabIndex = 1;
  203. this.groupBox1.TabStop = false;
  204. //
  205. // panel2
  206. //
  207. this.panel2.Controls.Add(this.label6);
  208. this.panel2.Controls.Add(this.label5);
  209. this.panel2.Controls.Add(this.label4);
  210. this.panel2.Controls.Add(this.linkLabel1);
  211. this.panel2.Controls.Add(this.label3);
  212. this.panel2.Controls.Add(this.label2);
  213. this.panel2.Controls.Add(this.groupBox3);
  214. this.panel2.Controls.Add(this.groupBox4);
  215. this.panel2.Location = new System.Drawing.Point(8, 264);
  216. this.panel2.Name = "panel2";
  217. this.panel2.Size = new System.Drawing.Size(112, 24);
  218. this.panel2.TabIndex = 5;
  219. //
  220. // label6
  221. //
  222. this.label6.AutoSize = true;
  223. this.label6.Location = new System.Drawing.Point(144, 208);
  224. this.label6.Name = "label6";
  225. this.label6.Size = new System.Drawing.Size(65, 12);
  226. this.label6.TabIndex = 14;
  227. this.label6.Text = "官方网站:";
  228. //
  229. // label5
  230. //
  231. this.label5.BackColor = System.Drawing.Color.Transparent;
  232. this.label5.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
  233. this.label5.Location = new System.Drawing.Point(4, 16);
  234. this.label5.Name = "label5";
  235. this.label5.Size = new System.Drawing.Size(136, 20);
  236. this.label5.TabIndex = 9;
  237. this.label5.Text = "感谢使用在线升级";
  238. //
  239. // label4
  240. //
  241. this.label4.Location = new System.Drawing.Point(144, 184);
  242. this.label4.Name = "label4";
  243. this.label4.Size = new System.Drawing.Size(100, 16);
  244. this.label4.TabIndex = 13;
  245. this.label4.Text = "夜空团队";
  246. //
  247. // linkLabel1
  248. //
  249. this.linkLabel1.Location = new System.Drawing.Point(210, 208);
  250. this.linkLabel1.Name = "linkLabel1";
  251. this.linkLabel1.Size = new System.Drawing.Size(250, 16);
  252. this.linkLabel1.TabIndex = 12;
  253. this.linkLabel1.TabStop = true;
  254. this.linkLabel1.Text = "www.chinetsoft.com";
  255. this.linkLabel1.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel1_LinkClicked);
  256. //
  257. // label3
  258. //
  259. this.label3.Location = new System.Drawing.Point(24, 130);
  260. this.label3.Name = "label3";
  261. this.label3.Size = new System.Drawing.Size(184, 16);
  262. this.label3.TabIndex = 11;
  263. this.label3.Text = " 欢迎继续关注我们的产品。";
  264. //
  265. // label2
  266. //
  267. this.label2.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
  268. this.label2.Location = new System.Drawing.Point(24, 64);
  269. this.label2.Name = "label2";
  270. this.label2.Size = new System.Drawing.Size(312, 66);
  271. this.label2.TabIndex = 10;
  272. this.label2.Text = " 程序更新完成!\r\n\r\n 点击\"完成\"按钮,程序会自动重新启动系统。";
  273. //
  274. // groupBox3
  275. //
  276. this.groupBox3.Dock = System.Windows.Forms.DockStyle.Bottom;
  277. this.groupBox3.Location = new System.Drawing.Point(0, 16);
  278. this.groupBox3.Name = "groupBox3";
  279. this.groupBox3.Size = new System.Drawing.Size(112, 8);
  280. this.groupBox3.TabIndex = 7;
  281. this.groupBox3.TabStop = false;
  282. //
  283. // groupBox4
  284. //
  285. this.groupBox4.Location = new System.Drawing.Point(0, 32);
  286. this.groupBox4.Name = "groupBox4";
  287. this.groupBox4.Size = new System.Drawing.Size(381, 8);
  288. this.groupBox4.TabIndex = 1;
  289. this.groupBox4.TabStop = false;
  290. //
  291. // tip
  292. //
  293. this.tip.AutoPopDelay = 5000;
  294. this.tip.InitialDelay = 500;
  295. this.tip.OwnerDraw = true;
  296. this.tip.ReshowDelay = 100;
  297. //
  298. // btnNext
  299. //
  300. this.btnNext.Location = new System.Drawing.Point(269, 264);
  301. this.btnNext.Name = "btnNext";
  302. this.btnNext.Size = new System.Drawing.Size(75, 23);
  303. this.btnNext.TabIndex = 7;
  304. this.btnNext.Text = "下一步(&N)";
  305. this.btnNext.UseVisualStyleBackColor = true;
  306. this.btnNext.Click += new System.EventHandler(this.btnNext_Click);
  307. //
  308. // btnCancel
  309. //
  310. this.btnCancel.Location = new System.Drawing.Point(410, 263);
  311. this.btnCancel.Name = "btnCancel";
  312. this.btnCancel.Size = new System.Drawing.Size(75, 23);
  313. this.btnCancel.TabIndex = 8;
  314. this.btnCancel.Text = "取 消(&C)";
  315. this.btnCancel.UseVisualStyleBackColor = true;
  316. this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
  317. //
  318. // btnFinish
  319. //
  320. this.btnFinish.Location = new System.Drawing.Point(138, 263);
  321. this.btnFinish.Name = "btnFinish";
  322. this.btnFinish.Size = new System.Drawing.Size(75, 23);
  323. this.btnFinish.TabIndex = 9;
  324. this.btnFinish.Text = "完成(&F)";
  325. this.btnFinish.UseVisualStyleBackColor = true;
  326. this.btnFinish.Click += new System.EventHandler(this.btnFinish_Click);
  327. //
  328. // btnClose
  329. //
  330. this.btnClose.Location = new System.Drawing.Point(220, 262);
  331. this.btnClose.Name = "btnClose";
  332. this.btnClose.Size = new System.Drawing.Size(75, 23);
  333. this.btnClose.TabIndex = 13;
  334. this.btnClose.Text = "关闭(&C)";
  335. this.btnClose.UseVisualStyleBackColor = true;
  336. this.btnClose.Click += new System.EventHandler(this.btnCancel_Click);
  337. //
  338. // FrmUpdate
  339. //
  340. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  341. this.ClientSize = new System.Drawing.Size(513, 301);
  342. this.ControlBox = false;
  343. this.Controls.Add(this.btnClose);
  344. this.Controls.Add(this.btnFinish);
  345. this.Controls.Add(this.btnCancel);
  346. this.Controls.Add(this.btnNext);
  347. this.Controls.Add(this.panel2);
  348. this.Controls.Add(this.panel1);
  349. this.Controls.Add(this.pictureBox1);
  350. this.DoubleBuffered = true;
  351. this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
  352. this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
  353. this.MaximizeBox = false;
  354. this.MinimizeBox = false;
  355. this.Name = "FrmUpdate";
  356. this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
  357. this.Text = "自动更新";
  358. this.Load += new System.EventHandler(this.FrmUpdate_Load);
  359. this.Shown += new System.EventHandler(this.FrmUpdate_Shown);
  360. ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
  361. this.panel1.ResumeLayout(false);
  362. this.panel2.ResumeLayout(false);
  363. this.panel2.PerformLayout();
  364. this.ResumeLayout(false);
  365. }
  366. #endregion
  367. private string updateUrl = string.Empty;
  368. private string tempUpdatePath = string.Empty;
  369. private string tempUpdatePath1 = string.Empty;
  370. XmlFiles updaterXmlFiles = null;
  371. private int availableUpdate = 0;
  372. bool isRun = false;
  373. string mainAppExe = "";
  374. /// <summary>
  375. /// 应用程序的主入口点。
  376. /// </summary>
  377. [STAThread]
  378. static void Main()
  379. {
  380. Application.EnableVisualStyles();
  381. Application.SetCompatibleTextRenderingDefault(false);
  382. Application.Run(new FrmUpdate());
  383. }
  384. #region Load事件
  385. private void FrmUpdate_Load(object sender, System.EventArgs e)
  386. {
  387. panel2.Visible = false;
  388. btnFinish.Visible = false;
  389. btnNext.Visible = false;
  390. btnClose.Visible = false;
  391. }
  392. #endregion
  393. private void btnCancel_Click(object sender, System.EventArgs e)
  394. {
  395. this.Close();
  396. Application.ExitThread();
  397. Application.Exit();
  398. }
  399. //点击"下一步"要执行的动作
  400. private void btnNext_Click(object sender, System.EventArgs e)
  401. {
  402. Thread threadDown = new Thread(new ThreadStart(DownUpdateFile));
  403. threadDown.IsBackground = true;
  404. threadDown.Start();
  405. btnNext.Enabled = false;
  406. }
  407. //下载可更新的文件
  408. private void DownUpdateFile()
  409. {
  410. mainAppExe = updaterXmlFiles.GetNodeValue("//EntryPoint");
  411. Process [] allProcess = Process.GetProcesses();
  412. foreach(Process p in allProcess)
  413. {
  414. if (p.ProcessName.ToLower() + ".exe" == mainAppExe.ToLower() )
  415. {
  416. for(int i=0;i<p.Threads.Count;i++)
  417. p.Threads[i].Dispose();
  418. p.Kill();
  419. isRun = true;
  420. //break;
  421. }
  422. }
  423. WebClient wcClient = new WebClient();
  424. for (int i = 0; i < this.lvUpdateList1.Items.Count; i++)
  425. {
  426. string UpdateFile = lvUpdateList1.Items[i].Text.Trim();
  427. string updateFileUrl = updateUrl + lvUpdateList1.Items[i].Text.Trim();
  428. long fileLength = 0;
  429. WebRequest webReq = WebRequest.Create(updateFileUrl);
  430. WebResponse webRes = webReq.GetResponse();
  431. fileLength = webRes.ContentLength;
  432. label1.Text = "正在下载更新文件,请稍后...";
  433. pbDownFile.Value = 0;
  434. pbDownFile.Maximum = (int)fileLength;
  435. try
  436. {
  437. Stream srm = webRes.GetResponseStream();
  438. StreamReader srmReader = new StreamReader(srm);
  439. byte[] bufferbyte = new byte[fileLength];
  440. int allByte = (int)bufferbyte.Length;
  441. int startByte = 0;
  442. while (fileLength > 0)
  443. {
  444. Application.DoEvents();
  445. int downByte = srm.Read(bufferbyte, startByte, allByte);
  446. if (downByte == 0) { break; };
  447. startByte += downByte;
  448. allByte -= downByte;
  449. pbDownFile.Value += downByte;
  450. float part = (float)startByte / 1024;
  451. float total = (float)bufferbyte.Length / 1024;
  452. int percent = Convert.ToInt32((part / total) * 100);
  453. this.lvUpdateList.Items[i].SubItems[2].Text = percent.ToString() + "%";
  454. }
  455. string tempPath = tempUpdatePath + UpdateFile;
  456. CreateDirtory(tempPath);
  457. FileStream fs = new FileStream(tempPath, FileMode.OpenOrCreate, FileAccess.Write);
  458. fs.Write(bufferbyte, 0, bufferbyte.Length);
  459. srm.Close();
  460. srmReader.Close();
  461. fs.Close();
  462. }
  463. catch (WebException ex)
  464. {
  465. MessageBox.Show("更新文件下载失败!" + ex.Message.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  466. }
  467. }
  468. InvalidateControl();
  469. //this.Cursor = Cursors.Default;
  470. }
  471. //创建目录
  472. private void CreateDirtory(string path)
  473. {
  474. if(!File.Exists(path))
  475. {
  476. string [] dirArray = path.Split('\\');
  477. string temp = string.Empty;
  478. for(int i = 0;i<dirArray.Length - 1;i++)
  479. {
  480. temp += dirArray[i].Trim() + "\\";
  481. if(!Directory.Exists(temp))
  482. Directory.CreateDirectory(temp);
  483. }
  484. }
  485. }
  486. //复制文件;
  487. public void CopyFile(string sourcePath,string objPath)
  488. {
  489. // char[] split = @"\".ToCharArray();
  490. if(!Directory.Exists(objPath))
  491. {
  492. Directory.CreateDirectory(objPath);
  493. }
  494. string[] files = Directory.GetFiles(sourcePath);
  495. for(int i=0;i<files.Length;i++)
  496. {
  497. string[] childfile = files[i].Split('\\');
  498. File.Copy(files[i],objPath + @"\" + childfile[childfile.Length-1],true);
  499. }
  500. string[] dirs = Directory.GetDirectories(sourcePath);
  501. for(int i=0;i<dirs.Length;i++)
  502. {
  503. string[] childdir = dirs[i].Split('\\');
  504. CopyFile(dirs[i],objPath + @"\" + childdir[childdir.Length-1]);
  505. }
  506. }
  507. private void linkLabel1_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
  508. {
  509. //打开艾软国际首页
  510. System.Diagnostics.Process.Start("http://chinetsoft.d209.cnaaa5.com");
  511. }
  512. //点击完成复制更新文件到应用程序目录
  513. private void btnFinish_Click(object sender, System.EventArgs e)
  514. {
  515. this.Close();
  516. this.Dispose();
  517. try
  518. {
  519. CopyFile(tempUpdatePath,Directory.GetCurrentDirectory() + "\\..\\");
  520. System.IO.Directory.Delete(tempUpdatePath,true);
  521. CopyFile(tempUpdatePath1, Directory.GetCurrentDirectory() + "\\..\\");
  522. System.IO.Directory.Delete(tempUpdatePath1, true);
  523. }
  524. catch(Exception ex)
  525. {
  526. MessageBox.Show(ex.Message.ToString());
  527. }
  528. if(true == this.isRun) Process.Start(".\\ISoft.exe");
  529. }
  530. //更新完毕重新绘制窗体部分控件属性
  531. private void InvalidateControl()
  532. {
  533. panel2.Location = panel1.Location;
  534. panel2.Size = panel1.Size;
  535. panel1.Visible = false;
  536. panel2.Visible = true;
  537. btnNext.Visible = false;
  538. btnCancel.Visible = false;
  539. btnFinish.Location = btnCancel.Location;
  540. btnFinish.Visible = true;
  541. }
  542. //检查无更新重新绘制窗体部分控件属性
  543. private void InvalidateControl1()
  544. {
  545. panel2.Location = panel1.Location;
  546. panel2.Size = panel1.Size;
  547. panel1.Visible = false;
  548. label2.Text = " 恭喜,您的程序已经是最新版!";
  549. panel2.Visible = true;
  550. btnCancel.Visible = false;
  551. btnClose.Location = btnCancel.Location;
  552. btnClose.Visible = true;
  553. }
  554. //判断主应用程序是否正在运行
  555. private bool IsMainAppRun()
  556. {
  557. string mainAppExe = updaterXmlFiles.GetNodeValue("//EntryPoint");
  558. bool isRun = false;
  559. Process [] allProcess = Process.GetProcesses();
  560. foreach(Process p in allProcess)
  561. {
  562. if (p.ProcessName.ToLower() + ".exe" == mainAppExe.ToLower() )
  563. {
  564. isRun = true;
  565. //break;
  566. }
  567. }
  568. return isRun;
  569. }
  570. //显示更新详细信息
  571. private void lvUpdateList_MouseMove(object sender, MouseEventArgs e)
  572. {
  573. ListViewItem item = lvUpdateList.GetItemAt(e.X, e.Y);
  574. if (item != null)
  575. {
  576. tip.Show(lvUpdateList.Items[item.Index].SubItems[3].Text, lvUpdateList, new Point(e.X + 20, e.Y + 20), 15000);
  577. tip.Active = true;
  578. }
  579. else
  580. {
  581. tip.Active = false;
  582. }
  583. }
  584. //窗体显示之后开始检查是否有更新
  585. private void FrmUpdate_Shown(object sender, EventArgs e)
  586. {
  587. Thread threadCheck = new Thread(new ThreadStart(XMLCheckUpdate));
  588. threadCheck.IsBackground = true;
  589. threadCheck.Start();
  590. }
  591. //对比XML文件检查是否有升级条目
  592. private void XMLCheckUpdate()
  593. {
  594. label1.Text = "正在检查更新,请稍候...";
  595. string localXmlFile = Application.StartupPath + "\\Update\\UpdateList.xml";
  596. string serverXmlFile = string.Empty;
  597. try
  598. {
  599. //从本地读取更新配置文件信息
  600. updaterXmlFiles = new XmlFiles(localXmlFile);
  601. }
  602. catch
  603. {
  604. MessageBox.Show("本地配置文件出错!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  605. this.Close();
  606. return;
  607. }
  608. //获取服务器地址
  609. updateUrl = updaterXmlFiles.GetNodeValue("//Url");
  610. AppUpdater appUpdater = new AppUpdater();
  611. appUpdater.UpdaterUrl = updateUrl + "/UpdateList.xml";
  612. appUpdater.UpdaterUrl1 = updateUrl + "/Update.xml";
  613. //与服务器连接,下载更新配置文件
  614. try
  615. {
  616. tempUpdatePath = Environment.GetEnvironmentVariable("Temp") + "\\" + "_" + updaterXmlFiles.FindNode("//Application").Attributes["applicationId"].Value + "_" + "y" + "_" + "x" + "_" + "m" + "_" + "\\";//非配置文件临时文件顶级路径
  617. tempUpdatePath1 = Environment.GetEnvironmentVariable("Temp") + "\\" + "_" + updaterXmlFiles.FindNode("//Application").Attributes["applicationId"].Value + "_" + "y" + "_" + "x" + "_" + "m" + "_" + "\\" + "ISoft\\Update\\";//配置文件临时文件路径
  618. appUpdater.DownAutoUpdateFile(tempUpdatePath1);
  619. }
  620. catch
  621. {
  622. MessageBox.Show("与服务器连接失败,操作超时!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  623. this.Close();
  624. return;
  625. }
  626. //获取更新文件列表
  627. Hashtable htUpdateFile = new Hashtable();
  628. serverXmlFile = tempUpdatePath1 + "\\UpdateList.xml";
  629. if (!File.Exists(serverXmlFile))
  630. {
  631. return;
  632. }
  633. availableUpdate = appUpdater.CheckForUpdate(serverXmlFile, localXmlFile, out htUpdateFile);
  634. if (availableUpdate > 0)
  635. {
  636. for (int i = 0; i < htUpdateFile.Count; i++)
  637. {
  638. string[] fileArray = (string[])htUpdateFile[i];
  639. lvUpdateList1.Items.Add(new ListViewItem(fileArray[0]));
  640. if (fileArray[0] == "ISoft\\ISoft.exe")
  641. {
  642. fileArray[0] = "主程序";
  643. lvUpdateList.Items.Add(new ListViewItem(fileArray));
  644. continue;
  645. }
  646. if (fileArray[0] == "ISoft\\Update\\AutoUpdate.exe")
  647. {
  648. fileArray[0] = "自动更新程序";
  649. lvUpdateList.Items.Add(new ListViewItem(fileArray));
  650. continue;
  651. }
  652. if (fileArray[0] == "ISoft\\AdvancedFunctions\\HotTools\\FileDestrory\\FileDestory.exe")
  653. {
  654. fileArray[0] = "文件粉碎机";
  655. lvUpdateList.Items.Add(new ListViewItem(fileArray));
  656. continue;
  657. }
  658. if (fileArray[0] == "ISoft\\AdvancedFunctions\\HotTools\\Individuation\\Individuation.exe")
  659. {
  660. fileArray[0] = "个性化";
  661. lvUpdateList.Items.Add(new ListViewItem(fileArray));
  662. continue;
  663. }
  664. if (fileArray[0] == "ISoft\\AdvancedFunctions\\HotTools\\RubbishCleaner\\RubbishCleaner.exe")
  665. {
  666. fileArray[0] = "垃圾清理";
  667. lvUpdateList.Items.Add(new ListViewItem(fileArray));
  668. continue;
  669. }
  670. if (fileArray[0] == "ISoft\\AdvancedFunctions\\SysOptimizedTools\\OSSpeed\\OSSpeed.exe")
  671. {
  672. fileArray[0] = "系统加速";
  673. lvUpdateList.Items.Add(new ListViewItem(fileArray));
  674. continue;
  675. }
  676. if (fileArray[0] == "ISoft\\AdvancedFunctions\\SysOptimizedTools\\ScanLargeFile\\ScanLargeFile.exe")
  677. {
  678. fileArray[0] = "大文件扫描";
  679. lvUpdateList.Items.Add(new ListViewItem(fileArray));
  680. continue;
  681. }
  682. if (fileArray[0] == "ISoft\\AdvancedFunctions\\SysTools\\DefaultSet\\DefaultSet.exe")
  683. {
  684. fileArray[0] = "默认软件设置";
  685. lvUpdateList.Items.Add(new ListViewItem(fileArray));
  686. continue;
  687. }
  688. if (fileArray[0] == "ISoft\\AdvancedFunctions\\SysTools\\ReadText\\ReadText.exe")
  689. {
  690. fileArray[0] = "语音朗读机";
  691. lvUpdateList.Items.Add(new ListViewItem(fileArray));
  692. continue;
  693. }
  694. if (fileArray[0] == "ISoft\\AdvancedFunctions\\SysTools\\FileMonitor\\FileMonitor.exe")
  695. {
  696. fileArray[0] = "磁盘监视";
  697. lvUpdateList.Items.Add(new ListViewItem(fileArray));
  698. continue;
  699. }
  700. if (fileArray[0] == "ISoft\\AdvancedFunctions\\SysTools\\TimeSyn\\TimeSyn.exe")
  701. {
  702. fileArray[0] = "时间同步助手";
  703. lvUpdateList.Items.Add(new ListViewItem(fileArray));
  704. continue;
  705. }
  706. if (fileArray[0] == "ISoft\\AdvancedFunctions\\OtherTools\\VideoRec\\VideoRec.exe")
  707. {
  708. fileArray[0] = "视频录制器";
  709. lvUpdateList.Items.Add(new ListViewItem(fileArray));
  710. continue;
  711. }
  712. if (fileArray[0] == "ISoft\\AdvancedFunctions\\OtherTools\\FileSplitAndJion\\FileSplitAndJion.exe")
  713. {
  714. fileArray[0] = "文件分割与合并";
  715. lvUpdateList.Items.Add(new ListViewItem(fileArray));
  716. continue;
  717. }
  718. if (fileArray[0] == "ISoft\\AdvancedFunctions\\OtherTools\\CompTxt\\CompTxt.exe")
  719. {
  720. fileArray[0] = "文件内容比较器";
  721. lvUpdateList.Items.Add(new ListViewItem(fileArray));
  722. continue;
  723. }
  724. if (fileArray[0] == "ISoft\\AdvancedFunctions\\OtherTools\\ThunderEncodeDecode\\ThunderEncodeDecode.exe")
  725. {
  726. fileArray[0] = "迅雷地址解码器";
  727. lvUpdateList.Items.Add(new ListViewItem(fileArray));
  728. continue;
  729. }
  730. }
  731. }
  732. if (availableUpdate == 0)
  733. {
  734. InvalidateControl1();
  735. }
  736. else
  737. {
  738. label1.Text = "以下为更新文件列表,请点击“下一步”开始更新";
  739. btnNext.Visible = true;
  740. }
  741. }
  742. private void lvUpdateList_MouseLeave(object sender, EventArgs e)
  743. {
  744. tip.Active = false;
  745. }
  746. }
  747. }