Program.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*******************************************************************************
  2. * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:Program.cs
  5. * 2.功能描述:系统启动主程序
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 张国印 2014/09/04 1.00 新建
  9. *******************************************************************************/
  10. using System;
  11. using System.Diagnostics;
  12. using System.IO;
  13. using System.Reflection;
  14. using System.Threading;
  15. using System.Windows.Forms;
  16. using Dongke.IBOSS.PRD.Basics.BaseResources;
  17. using Dongke.IBOSS.PRD.Basics.Library;
  18. using Dongke.IBOSS.PRD.Client.CommonModule;
  19. using Dongke.IBOSS.PRD.Client.DataModels;
  20. using Dongke.IBOSS.PRD.WCF.DataModels;
  21. using Dongke.IBOSS.PRD.WCF.Proxys;
  22. using Dongke.IBOSS.PRD.WCF.Proxys.DKIBOSSPRDService;
  23. namespace Dongke.IBOSS.PRD.Client
  24. {
  25. /// <summary>
  26. /// 系统启动主程序
  27. /// </summary>
  28. static class Program
  29. {
  30. #region 成员变量
  31. // 系统登录窗体
  32. private static F_SYS_0101 _frmLogin = null;
  33. // 主窗体title
  34. private static string _mainFormTitle = null;
  35. private static string _loginFormTitle = null;
  36. private static FileVersionInfo _info = null;
  37. // 程序是否已经登录
  38. private static bool _isLogin = false;
  39. // 配置文件的全路径
  40. private static string _iniFilePath = LocalPath.RootPath + Constant.INI_FILE_NAME;
  41. #endregion
  42. #region 属性
  43. /// <summary>
  44. /// 已经登录标识
  45. /// </summary>
  46. internal static bool IsLogin
  47. {
  48. get
  49. {
  50. return _isLogin;
  51. }
  52. set
  53. {
  54. _isLogin = value;
  55. }
  56. }
  57. #endregion
  58. /// <summary>
  59. /// 应用程序的主入口点。
  60. /// </summary>
  61. [STAThread]
  62. static void Main(string[] args)
  63. {
  64. // 检查程序是否已经在运行中
  65. bool isCreatedNew = true;
  66. Mutex ibossMutex = null;
  67. //#if DEBUG
  68. //#else
  69. // string ibossMutexName = SystemAPI.UserName + "-iboss.mes-";
  70. // ibossMutex = new Mutex(true, ibossMutexName, out isCreatedNew);
  71. //#endif
  72. // 取得程序的版本
  73. _info = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location);
  74. _mainFormTitle = Constant.M_SYSTEM_NAME + " - " + Constant.M_SYSTEM_CHINESE_SERIES + " - ver " + _info.FileVersion;
  75. _loginFormTitle = Constant.M_SYSTEM_CHINESE_NAME + " - ver " + _info.FileVersion;
  76. if (isCreatedNew)
  77. {
  78. try
  79. {
  80. Application.EnableVisualStyles();
  81. Application.SetCompatibleTextRenderingDefault(false);
  82. Application.ThreadException += Application_ThreadException;
  83. {
  84. // 更新自动更新程序
  85. if (Directory.Exists(LocalPath.InstallerDownloadPath))
  86. {
  87. string[] downFiles = Directory.GetFiles(LocalPath.InstallerDownloadPath);
  88. if (downFiles.Length > 0)
  89. {
  90. foreach (string name in downFiles)
  91. {
  92. string filename = Path.GetFileName(name);
  93. if (filename.StartsWith("AutoUpgrade")
  94. || "SharpZipLib.dll".Equals(filename))
  95. {
  96. string localFile = System.Environment.CurrentDirectory + @"\" + filename;
  97. if (File.Exists(localFile))
  98. {
  99. File.Delete(localFile);
  100. }
  101. File.Copy(name, localFile);
  102. File.Delete(name);
  103. }
  104. else
  105. {
  106. File.Delete(name);
  107. }
  108. }
  109. }
  110. }
  111. }
  112. // 系统登录
  113. Login(args);
  114. if (_isLogin)
  115. {
  116. //#if DEBUG
  117. // 如果升级版本发布错误,导致客户端都不能升级了。
  118. // 主程序
  119. //using (F_SYS_0201 main = new F_SYS_0201())
  120. //{
  121. // main.Text = _mainFormTitle;
  122. // Application.Run(main);
  123. //}
  124. //#else
  125. // 自动更新
  126. NeedUpgradeResultEntity needUpgradeInfo = DKIBOSSPRDProxy.Service.IsNeedUpgrade(_info.FileVersion);
  127. if (needUpgradeInfo.UpgradeState)
  128. {
  129. MessageBox.Show("检测到服务器上有最新版本,需要自动更新。",
  130. Messages.MSG_TITLE_I01,
  131. MessageBoxButtons.OK,
  132. MessageBoxIcon.Information);
  133. needUpgradeInfo.GradeInfo =
  134. string.Format("本地程序版本:{0}\r\n最新程序版本:{1}",
  135. _info.FileVersion, needUpgradeInfo.ServerVersion);
  136. // 如果有提示信息提示,没有就跳过
  137. if (!string.IsNullOrEmpty(needUpgradeInfo.GradeInfo))
  138. {
  139. F_SYS_0103 frmAutoUpgradeInfo = new F_SYS_0103(needUpgradeInfo.GradeInfo);
  140. DialogResult dialogResult = frmAutoUpgradeInfo.ShowDialog();
  141. if (dialogResult != DialogResult.OK)
  142. {
  143. Application.Exit();
  144. }
  145. else
  146. {
  147. Application.Exit();
  148. //Process.Start("AutoUpgrade.exe");
  149. Process.Start(System.AppDomain.CurrentDomain.BaseDirectory + "AutoUpgrade.exe", "IBOSSPRD.exe " + _frmLogin.Password);
  150. }
  151. }
  152. else
  153. {
  154. Application.Exit();
  155. //Process.Start("AutoUpgrade.exe");
  156. Process.Start(System.AppDomain.CurrentDomain.BaseDirectory + "AutoUpgrade.exe", "IBOSSPRD.exe " + _frmLogin.Password);
  157. }
  158. }
  159. // 如果有提示信息,不能连接服务器
  160. else if (!string.IsNullOrEmpty(needUpgradeInfo.GradeInfo))
  161. {
  162. MessageBox.Show(needUpgradeInfo.GradeInfo,
  163. Messages.MSG_TITLE_I01,
  164. MessageBoxButtons.OK,
  165. MessageBoxIcon.Warning);
  166. Application.Exit();
  167. }
  168. else
  169. {
  170. // 主程序
  171. using (F_SYS_0201 main = new F_SYS_0201())
  172. {
  173. main.Text = _mainFormTitle;
  174. Application.Run(main);
  175. }
  176. }
  177. //#endif
  178. }
  179. }
  180. catch (Exception ex)
  181. {
  182. // 对异常进行共通处理
  183. ExceptionManager.HandleEventException("Program.cs", "Main()", Messages.MSG_TITLE_E01, ex);
  184. }
  185. finally
  186. {
  187. if (_isLogin)
  188. {
  189. try
  190. {
  191. // 退出系统
  192. DKIBOSSPRDProxy.Service.Logout(LogInUserInfo.CurrentUser.UserID);
  193. }
  194. catch { }
  195. }
  196. if (ibossMutex != null)
  197. {
  198. ibossMutex.Close();
  199. }
  200. }
  201. }
  202. else
  203. {
  204. IntPtr iptWndMain = Utility.FindWindow(null, _mainFormTitle);
  205. if (iptWndMain == IntPtr.Zero)
  206. {
  207. IntPtr iptWndLogin = Utility.FindWindow(null, _loginFormTitle);
  208. if (iptWndLogin != IntPtr.Zero)
  209. {
  210. Utility.WakeupWindow(iptWndLogin);
  211. }
  212. }
  213. else
  214. {
  215. Utility.WakeupWindow(iptWndMain);
  216. }
  217. }
  218. }
  219. static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
  220. {
  221. //throw new NotImplementedException();
  222. ExceptionManager.HandleEventException(
  223. "Program.cs",
  224. "Application_ThreadException()",
  225. Messages.MSG_TITLE_E01,
  226. e.Exception);
  227. }
  228. #region 私有方法
  229. /// <summary>
  230. /// 调取系统登录页面进行登录验证
  231. /// </summary>
  232. /// <returns></returns>
  233. private static bool Login(string[] args)
  234. {
  235. _frmLogin = new F_SYS_0101();
  236. _frmLogin.Text = _loginFormTitle;
  237. if (args != null)
  238. {
  239. int argsLength = args.Length;
  240. if (argsLength == 1)
  241. {
  242. if ("IsChangedUser" == args[0])
  243. {
  244. _frmLogin.IsChangedUser = true;
  245. }
  246. }
  247. if (argsLength == 2)
  248. {
  249. if ("IBOSSPRD.exe" == args[0])
  250. {
  251. _frmLogin.Password = args[1];
  252. }
  253. }
  254. }
  255. DialogResult dialogResult = _frmLogin.ShowDialog();
  256. if (dialogResult == DialogResult.OK)
  257. {
  258. return true;
  259. }
  260. return false;
  261. }
  262. #endregion
  263. }
  264. }