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