UpdaterForm.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Collections;
  9. using System.IO;
  10. using System.Diagnostics;
  11. namespace FantasySolution.FantasyCorrector
  12. {
  13. public partial class UpdaterForm : Form
  14. {
  15. private IList UpdateLists;
  16. private ReleaseList LocalRelease = null;
  17. private ReleaseList RemoteRelease = null;
  18. private bool newRelaes = false;
  19. public UpdaterForm()
  20. {
  21. InitializeComponent();
  22. }
  23. private void UpdaterForm_Load(object sender, EventArgs e)
  24. {
  25. this.LocalRelease = new ReleaseList(Application.StartupPath + @"\" + GlobalVariable.Instance.ReleaseFileName);
  26. DownloadTools dt = new DownloadTools();
  27. textBox1.Text = this.LocalRelease.ReleaseVersion;
  28. textBox2.Text = this.LocalRelease.ReleaseDate;
  29. bool flg = dt.IsDownloadFile(GlobalVariable.Instance.LocalPath, GlobalVariable.Instance.ReleaseURL, GlobalVariable.Instance.ReleaseFileName);
  30. if (flg)
  31. {
  32. newRelaes = this.CheckUpdate();
  33. textBox3.Text = this.RemoteRelease.ReleaseVersion;
  34. textBox4.Text = this.RemoteRelease.ReleaseDate;
  35. textBox5.Text = this.RemoteRelease.ReleaseReason;
  36. }
  37. else
  38. {
  39. textBox3.Text = "未知";
  40. textBox4.Text = "未知";
  41. textBox5.Text = "未知";
  42. }
  43. }
  44. private bool CheckUpdate()
  45. {
  46. try
  47. {
  48. string fileNameAll = Application.StartupPath + "\\" + GlobalVariable.Instance.LocalPath + "\\" + GlobalVariable.Instance.ReleaseFileName;
  49. this.RemoteRelease = new ReleaseList(fileNameAll);
  50. if (this.LocalRelease==null||((this.LocalRelease != null) && (this.RemoteRelease != null)) && !this.LocalRelease.ReleaseDate.Equals(this.RemoteRelease.ReleaseDate))
  51. {
  52. return true;
  53. }
  54. }
  55. catch (Exception exception)
  56. {
  57. MessageBox.Show(exception.Message);
  58. }
  59. return false;
  60. }
  61. private void CloseButtonOnClick(object sender, EventArgs e)
  62. {
  63. this.Close();
  64. }
  65. private void UpdateButtonOnClick(object sender, EventArgs e)
  66. {
  67. if (this.newRelaes)
  68. {
  69. this.Init();
  70. if (this.StartDownload())
  71. {
  72. DialogResult result = MessageBox.Show("新版本更新完成,是否现在启动新程序?", Application.ProductName, MessageBoxButtons.YesNo);
  73. if (result == DialogResult.Yes)
  74. {
  75. this.Close();
  76. Application.Exit();
  77. GC.Collect();
  78. Process.Start(Application.StartupPath + @"\" + GlobalVariable.Instance.ApplicationStart);
  79. }
  80. }
  81. }
  82. else
  83. {
  84. MessageBox.Show("没有新版本,或者远程服务器无法连接!");
  85. }
  86. }
  87. public void Init()
  88. {
  89. this.textStatusLabel.Visible = true;
  90. this.progressBar.Visible = true;
  91. this.progressBar.Value = 0;
  92. this.statusStrip1.Update();
  93. IDictionary dictionary = new Hashtable();
  94. foreach (ReleaseFile file1 in this.LocalRelease.ReleaseFiles)
  95. {
  96. dictionary.Add(file1.FileName, file1);
  97. }
  98. this.UpdateLists = new ArrayList();
  99. foreach (ReleaseFile file2 in this.RemoteRelease.ReleaseFiles)
  100. {
  101. if (file2.FileName.Equals(GlobalVariable.Instance.ApplicationUpdater)) continue;
  102. ReleaseFile file3 = dictionary[file2.FileName] as ReleaseFile;
  103. if (file3 != null)
  104. {
  105. if (!file3.ReleaseDate.Equals(file2.ReleaseDate))
  106. {
  107. this.UpdateLists.Add(file2);
  108. }
  109. continue;
  110. }
  111. this.UpdateLists.Add(file2);
  112. }
  113. }
  114. private bool StartDownload()
  115. {
  116. string path = Application.StartupPath + @"\" + GlobalVariable.Instance.LocalPath;
  117. if (!Directory.Exists(path))
  118. {
  119. Directory.CreateDirectory(path);
  120. }
  121. IList list = new ArrayList();
  122. bool flag = true;
  123. this.progressBar.Maximum = 20 * this.UpdateLists.Count;
  124. int num = 20;
  125. foreach (ReleaseFile file in this.UpdateLists)
  126. {
  127. if (file == null)
  128. {
  129. continue;
  130. }
  131. string text3 = new DownloadTools().DownloadFile(path, GlobalVariable.Instance.ReleaseURL, file.FileName);
  132. if (text3 != null)
  133. {
  134. list.Add(text3);
  135. this.progressBar.Value += num;
  136. continue;
  137. }
  138. flag = false;
  139. break;
  140. }
  141. if (flag)
  142. {
  143. foreach (string text4 in list)
  144. {
  145. string sourceFileName = path + @"\" + text4;
  146. string destFileName = Application.StartupPath + @"\" + text4;
  147. if (text4.IndexOf(".exe.xml") >= 0)
  148. {
  149. destFileName = Application.StartupPath + @"\" + text4.Replace(".exe.xml",".exe.config");
  150. }
  151. File.Copy(sourceFileName, destFileName, true);
  152. File.Delete(sourceFileName);
  153. }
  154. string filename = Application.StartupPath + @"\" + GlobalVariable.Instance.ReleaseFileName;
  155. this.RemoteRelease.Save(filename);
  156. this.progressBar.Value = 20 * this.UpdateLists.Count;
  157. return true;
  158. }
  159. return false;
  160. }
  161. }
  162. }