61918412d653ad90aecc3df317013e7f1ccc4e15.svn-base 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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.Configuration;
  9. using System.Collections;
  10. using System.Diagnostics;
  11. using System.Net;
  12. using System.IO;
  13. namespace Mes.AutoUpdate
  14. {
  15. public partial class AutoUpdate : Form
  16. {
  17. private DataSet ServerSet;
  18. private DataSet LocalSet;
  19. public AutoUpdate()
  20. {
  21. InitializeComponent();
  22. }
  23. private void AutoUpdater_Load(object sender, EventArgs e)
  24. {
  25. Text += string.Format(" [{0}]", Application.ProductVersion.ToString());
  26. base.Visible = false;
  27. string hTTP_Address;
  28. try
  29. {
  30. hTTP_Address = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).AppSettings.Settings["downloadUrl"].Value.ToString();
  31. }
  32. catch
  33. {
  34. hTTP_Address = "http://172.16.0.108:9998/";
  35. }
  36. string fTP_Address;
  37. try
  38. {
  39. fTP_Address = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).AppSettings.Settings["downloadFtpUrl"].Value.ToString();
  40. }
  41. catch
  42. {
  43. fTP_Address = "ftp://172.16.0.108:21/";
  44. }
  45. string value;
  46. try
  47. {
  48. value = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).AppSettings.Settings["downloadUrlfile"].Value.ToString();
  49. }
  50. catch
  51. {
  52. value = "filelist.xml";
  53. }
  54. ArrayList arrayList = new ArrayList();
  55. ArrayList arrayList2 = new ArrayList();
  56. WebRequest.DefaultWebProxy = null;
  57. try
  58. {
  59. try
  60. {
  61. ArrayList arrayList3 = new ArrayList();
  62. arrayList3.Add(value);
  63. ArrayList files = arrayList3;
  64. DownManager.DownLoad(files, hTTP_Address, fTP_Address, null, null, out arrayList3);
  65. }
  66. catch (Exception ex)
  67. {
  68. throw new Exception("下载服务器更新列表失败!\n" + ex.Message);
  69. }
  70. ServerSet = new DataSet();
  71. ServerSet.ReadXml(Application.StartupPath + "\\filelist.xml");
  72. DataTable dataTable = ServerSet.Tables["file"];
  73. Hashtable hashtable = new Hashtable();
  74. LocalSet = new DataSet();
  75. foreach (DataRow row in dataTable.Rows)
  76. {
  77. string key = row["filename"].ToString().ToUpper();
  78. int result = 0;
  79. int.TryParse(row["var"].ToString(), out result);
  80. if (hashtable.ContainsKey(key) && (int)hashtable[key] < result)
  81. {
  82. hashtable[key] = result;
  83. }
  84. else
  85. {
  86. hashtable.Add(key, result);
  87. }
  88. }
  89. if (File.Exists(Application.StartupPath + "\\myfilelist.xml"))
  90. {
  91. LocalSet.ReadXml(Application.StartupPath + "\\myfilelist.xml");
  92. DataTable dataTable2 = new DataTable("file");
  93. if (LocalSet.Tables.Contains("file"))
  94. {
  95. dataTable2 = LocalSet.Tables["file"];
  96. }
  97. foreach (string key2 in hashtable.Keys)
  98. {
  99. DataRow[] array = new DataRow[0];
  100. if (dataTable2.Columns.Contains("filename"))
  101. {
  102. array = dataTable2.Select("filename='" + key2 + "'");
  103. }
  104. if (array != null && array.Length > 0)
  105. {
  106. int num = 0;
  107. int num2 = 0;
  108. for (int i = 0; i < array.Length; i++)
  109. {
  110. int result = Convert.ToInt32(array[0]["var"]);
  111. if (i == 0 || result > num)
  112. {
  113. num2 = i;
  114. num = result;
  115. }
  116. }
  117. if ((int)hashtable[key2] != num && !arrayList.Contains(key2))
  118. {
  119. arrayList.Add(key2);
  120. }
  121. }
  122. else if (!arrayList.Contains(key2))
  123. {
  124. arrayList.Add(key2);
  125. }
  126. }
  127. }
  128. else
  129. {
  130. foreach (string key3 in hashtable.Keys)
  131. {
  132. if (!arrayList.Contains(key3))
  133. {
  134. arrayList.Add(key3);
  135. }
  136. }
  137. }
  138. foreach (DataTable table in ServerSet.Tables)
  139. {
  140. if (!(table.TableName.ToUpper() == "FILE") && !LocalSet.Tables.Contains(table.TableName))
  141. {
  142. LocalSet.Tables.Add(table.Copy());
  143. }
  144. }
  145. if (arrayList.Count > 0)
  146. {
  147. StopProcess("Core.Mes.ClientManager");
  148. base.Visible = true;
  149. ArrayList arrayList4 = new ArrayList();
  150. string str = Convert.ToString(ServerSet.Tables["path"].Rows[0][0]);
  151. foreach (string item in arrayList)
  152. {
  153. arrayList4.Add(str + item);
  154. }
  155. arrayList2 = Updater(arrayList4, hTTP_Address, fTP_Address);
  156. DataTable dataTable4 = new DataTable("file");
  157. dataTable4.Columns.AddRange(new DataColumn[2]
  158. {
  159. new DataColumn("filename", typeof(string)),
  160. new DataColumn("var", typeof(string))
  161. });
  162. Hashtable hashtable2 = new Hashtable();
  163. if (LocalSet.Tables.Contains("file"))
  164. {
  165. foreach (DataRow row2 in LocalSet.Tables["file"].Rows)
  166. {
  167. string text = row2["filename"].ToString();
  168. int result2 = 0;
  169. int.TryParse(row2["var"].ToString(), out result2);
  170. if (!hashtable2.ContainsKey(text))
  171. {
  172. if (File.Exists(Path.Combine(Application.StartupPath, text)))
  173. {
  174. hashtable2.Add(text, result2);
  175. }
  176. }
  177. else if ((int)hashtable2[text] > result2)
  178. {
  179. hashtable2[text] = result2;
  180. }
  181. }
  182. }
  183. foreach (string item2 in arrayList2)
  184. {
  185. int result2 = (int)hashtable[item2];
  186. if (!hashtable2.ContainsKey(item2))
  187. {
  188. if (File.Exists(Path.Combine(Application.StartupPath, item2)))
  189. {
  190. hashtable2.Add(item2, result2);
  191. }
  192. }
  193. else
  194. {
  195. hashtable2[item2] = result2;
  196. }
  197. }
  198. foreach (string key4 in hashtable2.Keys)
  199. {
  200. DataRow dataRow2 = dataTable4.NewRow();
  201. dataRow2["filename"] = key4;
  202. dataRow2["var"] = hashtable2[key4].ToString();
  203. dataTable4.Rows.Add(dataRow2);
  204. }
  205. dataTable4.AcceptChanges();
  206. if (LocalSet.Tables.Contains("file"))
  207. {
  208. LocalSet.Tables.Remove("file");
  209. }
  210. LocalSet.AcceptChanges();
  211. LocalSet.Tables.Add(dataTable4);
  212. LocalSet.WriteXml(Application.StartupPath + "\\myfilelist.xml");
  213. }
  214. Process.Start(Application.StartupPath + "\\Core.Mes.ClientManager.exe");
  215. }
  216. catch (Exception ex)
  217. {
  218. MessageBox.Show(ex.Message);
  219. }
  220. finally
  221. {
  222. Close();
  223. Application.Exit();
  224. }
  225. }
  226. private ArrayList Updater(ArrayList files, string HTTP_Address, string FTP_Address)
  227. {
  228. ArrayList Finished = null;
  229. lock (files)
  230. {
  231. try
  232. {
  233. DownManager.DownLoad(files, HTTP_Address, FTP_Address, Main_Progress, Show_Progress, out Finished);
  234. }
  235. catch (Exception ex)
  236. {
  237. MessageBox.Show(ex.Message, "更新出错!", MessageBoxButtons.OK, MessageBoxIcon.Hand);
  238. }
  239. }
  240. return Finished;
  241. }
  242. private void Main_Progress(int _A, int _B)
  243. {
  244. Application.DoEvents();
  245. progressBar1.Maximum = _B;
  246. progressBar1.Value = _A;
  247. }
  248. private void Show_Progress(string main_progress, string sub_progress)
  249. {
  250. Application.DoEvents();
  251. lblfile.Text = string.Format("{0} [{1}]", main_progress, sub_progress);
  252. }
  253. private void StopProcess(string processName)
  254. {
  255. try
  256. {
  257. Process[] processesByName = Process.GetProcessesByName(processName);
  258. Process[] array = processesByName;
  259. foreach (Process process in array)
  260. {
  261. if (processName.Length >= 4 && processName.Substring(processName.Length - 4, 4).ToUpper() != ".EXE")
  262. {
  263. processName += ".EXE";
  264. }
  265. string path = Path.Combine(Application.StartupPath, processName);
  266. if (Path.GetFullPath(process.MainModule.FileName).ToUpper() == Path.GetFullPath(path).ToUpper())
  267. {
  268. if (MessageBox.Show("检测到客户端正在运行,是否强制关闭?", "警告", MessageBoxButtons.OKCancel) == DialogResult.OK)
  269. {
  270. process.Kill();
  271. }
  272. else
  273. {
  274. Close();
  275. Application.Exit();
  276. }
  277. }
  278. }
  279. }
  280. catch (InvalidOperationException ex)
  281. {
  282. Debug.Print(ex.Message);
  283. }
  284. catch (Exception ex2)
  285. {
  286. throw ex2;
  287. }
  288. }
  289. }
  290. }