361adc44292cfb279f55a326365117f8a6542b2e.svn-base 14 KB

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