Program.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. Curtain.Log.Logger.DefaultLogDirectory = LocalPath.LogRootPath;
  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. if (args != null)
  91. {
  92. System.Threading.Thread.Sleep(3000);
  93. }
  94. try
  95. {
  96. foreach (string name in downFiles)
  97. {
  98. string filename = Path.GetFileName(name);
  99. if (filename.StartsWith("AutoUpgrade")
  100. || "SharpZipLib.dll".Equals(filename))
  101. {
  102. string localFile = System.Environment.CurrentDirectory + @"\" + filename;
  103. if (File.Exists(localFile))
  104. {
  105. File.Delete(localFile);
  106. }
  107. File.Copy(name, localFile);
  108. File.Delete(name);
  109. }
  110. else
  111. {
  112. File.Delete(name);
  113. }
  114. }
  115. }
  116. catch (Exception ex)
  117. {
  118. Curtain.Log.Logger.Error(ex);
  119. }
  120. }
  121. }
  122. }
  123. // 系统登录
  124. Login(args);
  125. if (_isLogin)
  126. {
  127. //#if DEBUG
  128. // 如果升级版本发布错误,导致客户端都不能升级了。
  129. // 主程序
  130. //using (F_SYS_0201 main = new F_SYS_0201())
  131. //{
  132. // main.Text = _mainFormTitle;
  133. // Application.Run(main);
  134. //}
  135. //#else
  136. // 自动更新
  137. NeedUpgradeResultEntity needUpgradeInfo = DKIBOSSPRDProxy.Service.IsNeedUpgrade(_info.FileVersion);
  138. if (needUpgradeInfo.UpgradeState)
  139. {
  140. MessageBox.Show("检测到服务器上有最新版本,需要自动更新。",
  141. Messages.MSG_TITLE_I01,
  142. MessageBoxButtons.OK,
  143. MessageBoxIcon.Information);
  144. needUpgradeInfo.GradeInfo =
  145. string.Format("本地程序版本:{0}\r\n最新程序版本:{1}",
  146. _info.FileVersion, needUpgradeInfo.ServerVersion);
  147. // 如果有提示信息提示,没有就跳过
  148. if (!string.IsNullOrEmpty(needUpgradeInfo.GradeInfo))
  149. {
  150. F_SYS_0103 frmAutoUpgradeInfo = new F_SYS_0103(needUpgradeInfo.GradeInfo);
  151. DialogResult dialogResult = frmAutoUpgradeInfo.ShowDialog();
  152. if (dialogResult != DialogResult.OK)
  153. {
  154. Application.Exit();
  155. }
  156. else
  157. {
  158. Application.Exit();
  159. //Process.Start("AutoUpgrade.exe");
  160. Process.Start(System.AppDomain.CurrentDomain.BaseDirectory + "AutoUpgrade.exe", "IBOSSPRD.exe " + _frmLogin.Password);
  161. }
  162. }
  163. else
  164. {
  165. Application.Exit();
  166. //Process.Start("AutoUpgrade.exe");
  167. Process.Start(System.AppDomain.CurrentDomain.BaseDirectory + "AutoUpgrade.exe", "IBOSSPRD.exe " + _frmLogin.Password);
  168. }
  169. }
  170. // 如果有提示信息,不能连接服务器
  171. else if (!string.IsNullOrEmpty(needUpgradeInfo.GradeInfo))
  172. {
  173. MessageBox.Show(needUpgradeInfo.GradeInfo,
  174. Messages.MSG_TITLE_I01,
  175. MessageBoxButtons.OK,
  176. MessageBoxIcon.Warning);
  177. Application.Exit();
  178. }
  179. else
  180. {
  181. // 主程序
  182. using (F_SYS_0201 main = new F_SYS_0201())
  183. {
  184. main.Text = _mainFormTitle;
  185. Application.Run(main);
  186. }
  187. }
  188. //#endif
  189. }
  190. }
  191. catch (Exception ex)
  192. {
  193. // 对异常进行共通处理
  194. ExceptionManager.HandleEventException("Program.cs", "Main()", Messages.MSG_TITLE_E01, ex);
  195. }
  196. finally
  197. {
  198. if (_isLogin)
  199. {
  200. try
  201. {
  202. // 退出系统
  203. DKIBOSSPRDProxy.Service.Logout(LogInUserInfo.CurrentUser.UserID);
  204. }
  205. catch { }
  206. }
  207. if (ibossMutex != null)
  208. {
  209. ibossMutex.Close();
  210. }
  211. }
  212. }
  213. else
  214. {
  215. IntPtr iptWndMain = Utility.FindWindow(null, _mainFormTitle);
  216. if (iptWndMain == IntPtr.Zero)
  217. {
  218. IntPtr iptWndLogin = Utility.FindWindow(null, _loginFormTitle);
  219. if (iptWndLogin != IntPtr.Zero)
  220. {
  221. Utility.WakeupWindow(iptWndLogin);
  222. }
  223. }
  224. else
  225. {
  226. Utility.WakeupWindow(iptWndMain);
  227. }
  228. }
  229. }
  230. static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
  231. {
  232. //throw new NotImplementedException();
  233. ExceptionManager.HandleEventException(
  234. "Program.cs",
  235. "Application_ThreadException()",
  236. Messages.MSG_TITLE_E01,
  237. e.Exception);
  238. }
  239. #region 私有方法
  240. /// <summary>
  241. /// 调取系统登录页面进行登录验证
  242. /// </summary>
  243. /// <returns></returns>
  244. private static bool Login(string[] args)
  245. {
  246. _frmLogin = new F_SYS_0101();
  247. _frmLogin.Text = _loginFormTitle;
  248. if (args != null)
  249. {
  250. int argsLength = args.Length;
  251. if (argsLength == 1)
  252. {
  253. if ("IsChangedUser" == args[0])
  254. {
  255. _frmLogin.IsChangedUser = true;
  256. }
  257. }
  258. if (argsLength == 2)
  259. {
  260. if ("IBOSSPRD.exe" == args[0])
  261. {
  262. _frmLogin.Password = args[1];
  263. }
  264. }
  265. }
  266. DialogResult dialogResult = _frmLogin.ShowDialog();
  267. if (dialogResult == DialogResult.OK)
  268. {
  269. return true;
  270. }
  271. return false;
  272. }
  273. #endregion
  274. }
  275. }