FrmAutoUpgradeInfomation.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. /*******************************************************************************
  2. * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:FrmAutoUpgradeInfomation.cs
  5. * 2.功能描述:系统自动更新画面
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 陈晓野 2014/09/20 1.00 新建
  9. *******************************************************************************/
  10. using System;
  11. using System.Diagnostics;
  12. using System.IO;
  13. using System.Runtime.InteropServices;
  14. using System.ServiceModel;
  15. using System.Text;
  16. using System.Threading;
  17. using System.Windows.Forms;
  18. using Dongke.IBOSS.PRD.Client.AutoUpgrade.DKIBOSSPRDService;
  19. namespace Dongke.IBOSS.PRD.Client.AutoUpgrade
  20. {
  21. /// <summary>
  22. /// 系统自动更新画面
  23. /// </summary>
  24. public partial class FrmAutoUpgradeInfomation : Form
  25. {
  26. #region 系统DLL引用
  27. /// <summary>
  28. /// 读取INI文件
  29. /// </summary>
  30. /// <param name="section">节点名称</param>
  31. /// <param name="key">键</param>
  32. /// <param name="def">值</param>
  33. /// <param name="retval">stringbulider对象</param>
  34. /// <param name="size">字节大小</param>
  35. /// <param name="filePath">文件路径</param>
  36. /// <returns></returns>
  37. [DllImport("kernel32")]
  38. private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retval, int size, string filePath);
  39. [DllImport("user32.dll", EntryPoint = "GetSystemMenu")]
  40. public static extern IntPtr GetSystemMenu(IntPtr hWnd, int bRevert);
  41. [DllImport("User32.dll")]
  42. public static extern bool EnableMenuItem(IntPtr hMenu, int uIDEnableItem, int uEnable);
  43. #endregion
  44. #region 常量
  45. public const int SW_RESTORE = 9;
  46. public const int SC_CLOSE = 0xF060;
  47. public const int MF_ENABLED = 0x00000000;
  48. public const int MF_GRAYED = 0x00000001;
  49. public const int MF_DISABLED = 0x00000002;
  50. public const int WM_SYSCOMMAND = 0x112;
  51. public const int WM_QUERYENDSESSION = 0x11;
  52. public const int WM_CUSTOM_FW = 0x1024;
  53. // 配置文件名
  54. private const string INI_FILE_NAME = "iBossPrdConfig.ini";
  55. // 配置文件业务配置的域名
  56. private const string INI_SECTION_NAME = "NetworkAddress";
  57. // IP地址节点的KEY
  58. private const string INI_IP_KEY = "IP";
  59. // 端口节点的KEY
  60. private const string INI_PORT_KEY = "Port";
  61. // 系统的地址
  62. private const string SYSTEM_WEB_ADDRESS = "http://{0}:{1}/DKService/{2}";
  63. // 主程序名
  64. private const string MAIN_NAME = "IBOSSPRD.exe";
  65. // 主程序名
  66. private const string PUBLIC_MAIN_NAME = "IBOSSPRDPublic.exe";
  67. // 更新服务名
  68. private const string SERVICE_NAME = "DKIBOSSPRDService";
  69. private const string PUBLIC_SERVICE_NAME = "PublicModuleService";
  70. // 缓存及配置文件路径
  71. private const string SETTING_PATH = @"\DongkeSoft\IBOSSPRD\";
  72. #endregion
  73. #region 成员变量
  74. // INI文件的路径
  75. private string _iniFilePath;
  76. private string _localiniFilePath;
  77. // 下载到客户端的地址
  78. private string _downloadFolder;
  79. // 应用程序文件夹
  80. private string _applicationFolder;
  81. // 服务信道
  82. private IDKIBOSSPRD _channel = null;
  83. #endregion
  84. #region 委托声明
  85. private delegate void SetPos();
  86. private delegate void SetContrl(Label lable);
  87. private delegate void SetDownFileValue(int value);
  88. private delegate object AsyncMethod();
  89. #endregion
  90. #region 构造函数
  91. /// <summary>
  92. /// 构造函数
  93. /// </summary>
  94. public FrmAutoUpgradeInfomation()
  95. {
  96. try
  97. {
  98. if (Program.IsPublic)
  99. {
  100. if (File.Exists(System.AppDomain.CurrentDomain.BaseDirectory + MAIN_NAME))
  101. {
  102. Program.IsPublic = false;
  103. }
  104. }
  105. InitializeComponent();
  106. this.lblCon1.Visible = false;
  107. this.lblCon2.Visible = false;
  108. this.lblCon3.Visible = false;
  109. this.lblOk1.Visible = false;
  110. this.lblOk2.Visible = false;
  111. this.lblOk3.Visible = false;
  112. this.txtUpgradeInfo.ReadOnly = true;
  113. this.btnFinish.Visible = false;
  114. _applicationFolder = System.Environment.CurrentDirectory;
  115. StringBuilder personalDataSavePath = new StringBuilder();
  116. personalDataSavePath.Append(System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData));
  117. personalDataSavePath.Append(SETTING_PATH);
  118. _iniFilePath = personalDataSavePath.ToString() + INI_FILE_NAME;
  119. if (Program.IsPublic)
  120. {
  121. _downloadFolder = personalDataSavePath.ToString() + @"UpPublicDownload\";
  122. }
  123. else
  124. {
  125. _downloadFolder = personalDataSavePath.ToString() + @"UpDownload\";
  126. }
  127. _localiniFilePath = AppDomain.CurrentDomain.BaseDirectory + INI_FILE_NAME;
  128. //string ipAddress = this.ReadIniFile(INI_SECTION_NAME, INI_IP_KEY, _iniFilePath);
  129. //string serverPort = this.ReadIniFile(INI_SECTION_NAME, INI_PORT_KEY, _iniFilePath);
  130. string ipAddress = null;
  131. string serverPort = null;
  132. if (File.Exists(_localiniFilePath))
  133. {
  134. ipAddress = this.ReadIniFile(INI_SECTION_NAME, INI_IP_KEY, _localiniFilePath);
  135. serverPort = this.ReadIniFile(INI_SECTION_NAME, INI_PORT_KEY, _localiniFilePath);
  136. }
  137. else
  138. {
  139. ipAddress = this.ReadIniFile(INI_SECTION_NAME, INI_IP_KEY, _iniFilePath);
  140. serverPort = this.ReadIniFile(INI_SECTION_NAME, INI_PORT_KEY, _iniFilePath);
  141. }
  142. string sname = null;
  143. if (Program.IsPublic)
  144. {
  145. sname = SERVICE_NAME;
  146. }
  147. else
  148. {
  149. sname = SERVICE_NAME;
  150. }
  151. string uri = string.Format(SYSTEM_WEB_ADDRESS, ipAddress, serverPort, sname);
  152. EndpointAddress remoteAddress = new EndpointAddress(uri);
  153. ChannelFactory<IDKIBOSSPRD> channelFactory = new ChannelFactory<IDKIBOSSPRD>(sname, remoteAddress);
  154. channelFactory.Open();
  155. _channel = channelFactory.CreateChannel();
  156. }
  157. catch (Exception ex)
  158. {
  159. MessageBox.Show(string.Format("加载升级页面时出现了如下错误:\r\n{0}", ex.Message), this.Text, MessageBoxButtons.OK,
  160. MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
  161. }
  162. }
  163. #endregion
  164. #region 事件处理
  165. /// <summary>
  166. /// 窗体加载
  167. /// </summary>
  168. /// <param name="sender"></param>
  169. /// <param name="e"></param>
  170. private void FrmAutoUpgradeInfomation_Shown(object sender, EventArgs e)
  171. {
  172. try
  173. {
  174. object obj = DoAsync(new AsyncMethod(this.LoadAutoUpgrade));
  175. this.btnFinish.Visible = true;
  176. //if (Program.IsPublic)
  177. {
  178. this.btnFinish_Click(null, null);
  179. }
  180. }
  181. catch (Exception ex)
  182. {
  183. MessageBox.Show(string.Format("加载升级页面时出现了如下错误:\r\n{0}", ex.Message), this.Text, MessageBoxButtons.OK,
  184. MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
  185. }
  186. }
  187. /// <summary>
  188. /// 完成事件
  189. /// </summary>
  190. /// <param name="sender"></param>
  191. /// <param name="e"></param>
  192. private void btnFinish_Click(object sender, EventArgs e)
  193. {
  194. Application.Exit();
  195. //Process.Start(MAIN_NAME);
  196. if (Program.IsPublic)
  197. {
  198. Process.Start(System.AppDomain.CurrentDomain.BaseDirectory + PUBLIC_MAIN_NAME);
  199. }
  200. else
  201. {
  202. Process.Start(System.AppDomain.CurrentDomain.BaseDirectory + MAIN_NAME, Program.Parameter);
  203. }
  204. }
  205. /// <summary>
  206. /// 关闭操作
  207. /// </summary>
  208. /// <param name="sender"></param>
  209. /// <param name="e"></param>
  210. private void btnClose_Click(object sender, EventArgs e)
  211. {
  212. this.Close();
  213. }
  214. #endregion
  215. #region 私有方法
  216. /// <summary>
  217. /// 设置关闭按钮可用状态
  218. /// </summary>
  219. /// <param name="value"></param>
  220. private void SetCloseBox(bool value)
  221. {
  222. if (this.Handle == System.IntPtr.Zero)
  223. {
  224. return;
  225. }
  226. IntPtr hMenu = GetSystemMenu(this.Handle, 0);
  227. if (hMenu == System.IntPtr.Zero)
  228. {
  229. return;
  230. }
  231. if (value)
  232. {
  233. EnableMenuItem(hMenu, SC_CLOSE, MF_ENABLED);
  234. }
  235. else
  236. {
  237. EnableMenuItem(hMenu, SC_CLOSE, MF_DISABLED | MF_GRAYED);
  238. }
  239. return;
  240. }
  241. /// <summary>
  242. /// 窗体的异步处理
  243. /// </summary>
  244. /// <param name="method">异步方法</param>
  245. /// <returns></returns>
  246. private object DoAsync(AsyncMethod method)
  247. {
  248. object result = null;
  249. try
  250. {
  251. this.SetCloseBox(false);
  252. IAsyncResult asyncResult = method.BeginInvoke(null, null);
  253. while (!asyncResult.IsCompleted)
  254. {
  255. Application.DoEvents();
  256. System.Threading.Thread.Sleep(1);
  257. }
  258. result = method.EndInvoke(asyncResult);
  259. }
  260. finally
  261. {
  262. this.SetCloseBox(true);
  263. try
  264. {
  265. AsyncMethod endProgressMethod = new AsyncMethod(EndDoAsync);
  266. IAsyncResult ar = endProgressMethod.BeginInvoke(null, null);
  267. while (!ar.IsCompleted)
  268. {
  269. Application.DoEvents();
  270. System.Threading.Thread.Sleep(1);
  271. }
  272. endProgressMethod.EndInvoke(ar);
  273. }
  274. catch (Exception ex)
  275. {
  276. throw (ex);
  277. }
  278. }
  279. return result;
  280. }
  281. /// <summary>
  282. /// 异步处理结束
  283. /// </summary>
  284. /// <returns></returns>
  285. private object EndDoAsync()
  286. {
  287. return null;
  288. }
  289. /// <summary>
  290. /// 读取INI文件
  291. /// </summary>
  292. /// <param name="section">节点名称</param>
  293. /// <param name="key">键</param>
  294. /// <param name="filePath">文件路径</param>
  295. /// <returns></returns>
  296. private string ReadIniFile(string section, string key, string filePath)
  297. {
  298. try
  299. {
  300. StringBuilder returnValue = new StringBuilder(1024);
  301. GetPrivateProfileString(section, key, "", returnValue, 1024, filePath);
  302. return returnValue.ToString();
  303. }
  304. catch (Exception ex)
  305. {
  306. throw ex;
  307. }
  308. }
  309. /// <summary>
  310. /// 异步设置控件
  311. /// </summary>
  312. /// <param name="label"></param>
  313. private void SetLabel(Label label)
  314. {
  315. if (this.InvokeRequired)
  316. {
  317. SetContrl setpos = new SetContrl(SetLabel);
  318. this.Invoke(setpos, new object[] { label });
  319. }
  320. else
  321. {
  322. label.Visible = true;
  323. label.BringToFront();
  324. }
  325. }
  326. /// <summary>
  327. /// 设置进度条
  328. /// </summary>
  329. /// <param name="label"></param>
  330. private void SetProcess()
  331. {
  332. if (this.InvokeRequired)
  333. {
  334. SetPos setpos = new SetPos(SetProcess);
  335. this.Invoke(setpos);
  336. }
  337. else
  338. {
  339. for (int i = 0; i < 15; i++)
  340. {
  341. this.pbDownFile.Value = i;
  342. Thread.Sleep(100);
  343. }
  344. }
  345. }
  346. /// <summary>
  347. /// 下载文件的主程序
  348. /// </summary>
  349. private void DownLoadFile()
  350. {
  351. try
  352. {
  353. SetLabel(this.lblCon1);
  354. if (!Directory.Exists(_downloadFolder))
  355. {
  356. Directory.CreateDirectory(_downloadFolder);
  357. }
  358. string downloadDir = _downloadFolder + "-Upgrade.up";
  359. // 下载更新文件
  360. SetProcess();
  361. ICommunicationObject communicationObject = (ICommunicationObject)_channel;
  362. byte[] downLoadFile = null;
  363. try
  364. {
  365. if (Program.IsPublic)
  366. {
  367. downLoadFile = _channel.DownloadUpgradeFile("Public");
  368. }
  369. else
  370. {
  371. downLoadFile = _channel.DownloadUpgradeFile(null);
  372. }
  373. communicationObject.Close();
  374. }
  375. catch (Exception ex)
  376. {
  377. communicationObject.Abort();
  378. throw ex;
  379. }
  380. // 流转换成压缩文件
  381. File.WriteAllBytes(downloadDir, downLoadFile);
  382. SetLabel(this.lblOk1);
  383. //this.pbDownFile.Value = 20;
  384. SetDownFile(20);
  385. // 正在解压更新文件
  386. SetLabel(this.lblCon2);
  387. ZipFileClass.UnZipFile(downloadDir, _downloadFolder);
  388. SetLabel(this.lblOk2);
  389. //this.pbDownFile.Value = 30;
  390. SetDownFile(30);
  391. // 将需要更新的文件复制到运行文件夹下
  392. bool hasEXE = false;
  393. string exeFname = null;
  394. string upFname = null;
  395. SetLabel(this.lblCon3);
  396. string[] downFiles = Directory.GetFiles(_downloadFolder);
  397. int x = 55 / downFiles.Length;
  398. string mainName = MAIN_NAME;
  399. if (Program.IsPublic)
  400. {
  401. mainName = PUBLIC_MAIN_NAME;
  402. }
  403. foreach (string name in downFiles)
  404. {
  405. string filename = Path.GetFileName(name);
  406. if (filename.StartsWith("AutoUpgrade")
  407. || "SharpZipLib.dll".Equals(filename)
  408. || filename.EndsWith(".ini"))
  409. {
  410. continue;
  411. }
  412. if (mainName == filename)
  413. {
  414. hasEXE = true;
  415. exeFname = name;
  416. continue;
  417. }
  418. if ("-Upgrade.up".Equals(filename))
  419. {
  420. upFname = name;
  421. continue;
  422. }
  423. //if (!"SharpZipLib.dll".Equals(filename))
  424. {
  425. string localFile = _applicationFolder + @"\" + filename;
  426. if (File.Exists(localFile))
  427. {
  428. File.Delete(localFile);
  429. }
  430. File.Copy(name, localFile);
  431. File.Delete(name);
  432. }
  433. //this.pbDownFile.Value = 30 + x;
  434. SetDownFile(30 + x);
  435. }
  436. if (File.Exists(upFname))
  437. {
  438. File.Delete(upFname);
  439. }
  440. // 最后更新exe文件
  441. if (hasEXE)
  442. {
  443. string localFileIBOSS = _applicationFolder + @"\" + mainName;
  444. if (File.Exists(localFileIBOSS))
  445. {
  446. File.Delete(localFileIBOSS);
  447. }
  448. File.Copy(exeFname, localFileIBOSS);
  449. File.Delete(exeFname);
  450. //this.pbDownFile.Value = 100;
  451. SetDownFile(100);
  452. }
  453. SetLabel(this.lblOk3);
  454. //MessageBox.Show("更新完成", this.Text, MessageBoxButtons.OK,
  455. // MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
  456. }
  457. catch (Exception ex)
  458. {
  459. throw ex;
  460. }
  461. }
  462. /// <summary>
  463. /// 加载自动更新信息
  464. /// </summary>
  465. private object LoadAutoUpgrade()
  466. {
  467. try
  468. {
  469. this.DownLoadFile();
  470. return null;
  471. }
  472. catch (Exception ex)
  473. {
  474. throw ex;
  475. }
  476. }
  477. /// <summary>
  478. /// 设置进度条
  479. /// </summary>
  480. /// <param name="value"></param>
  481. private void SetDownFile(int value)
  482. {
  483. try
  484. {
  485. if (this.InvokeRequired)
  486. {
  487. SetDownFileValue setpos = new SetDownFileValue(SetDownFile);
  488. this.Invoke(setpos, new object[] { value });
  489. }
  490. else
  491. {
  492. this.pbDownFile.Value = value;
  493. }
  494. }
  495. catch (Exception ex)
  496. {
  497. throw ex;
  498. }
  499. }
  500. #endregion
  501. }
  502. }