/*******************************************************************************
* Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
* 类的信息:
* 1.程序名称:Program.cs
* 2.功能描述:系统启动主程序
* 编辑履历:
* 作者 日期 版本 修改内容
* 张国印 2014/09/04 1.00 新建
*******************************************************************************/
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Windows.Forms;
using Dongke.IBOSS.PRD.Basics.BaseResources;
using Dongke.IBOSS.PRD.Basics.Library;
using Dongke.IBOSS.PRD.Client.CommonModule;
using Dongke.IBOSS.PRD.Client.DataModels;
using Dongke.IBOSS.PRD.WCF.DataModels;
using Dongke.IBOSS.PRD.WCF.Proxys;
namespace Dongke.IBOSS.PRD.Client
{
///
/// 系统启动主程序
///
static class Program
{
#region 成员变量
// 系统登录窗体
private static F_SYS_0101 _frmLogin = null;
// 主窗体title
private static string _mainFormTitle = null;
private static string _loginFormTitle = null;
private static FileVersionInfo _info = null;
// 程序是否已经登录
private static bool _isLogin = false;
// 配置文件的全路径
private static string _iniFilePath = LocalPath.RootPath + Constant.INI_FILE_NAME;
#endregion
#region 属性
///
/// 已经登录标识
///
internal static bool IsLogin
{
get
{
return _isLogin;
}
set
{
_isLogin = value;
}
}
#endregion
///
/// 应用程序的主入口点。
///
[STAThread]
static void Main(string[] args)
{
Curtain.Log.Logger.DefaultLogDirectory = LocalPath.LogRootPath;
// 检查程序是否已经在运行中
bool isCreatedNew = true;
Mutex ibossMutex = null;
//#if DEBUG
//#else
// string ibossMutexName = SystemAPI.UserName + "-iboss.mes-";
// ibossMutex = new Mutex(true, ibossMutexName, out isCreatedNew);
//#endif
// 取得程序的版本
_info = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location);
_mainFormTitle = Constant.M_SYSTEM_NAME + " - " + Constant.M_SYSTEM_CHINESE_SERIES + " - ver " + _info.FileVersion;
_loginFormTitle = Constant.M_SYSTEM_CHINESE_NAME + " - ver " + _info.FileVersion;
if (isCreatedNew)
{
try
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.ThreadException += Application_ThreadException;
{
// 更新自动更新程序
if (Directory.Exists(LocalPath.InstallerDownloadPath))
{
string[] downFiles = Directory.GetFiles(LocalPath.InstallerDownloadPath);
if (downFiles.Length > 0)
{
if (args != null)
{
System.Threading.Thread.Sleep(3000);
}
try
{
foreach (string name in downFiles)
{
string filename = Path.GetFileName(name);
if (filename.StartsWith("AutoUpgrade")
|| "SharpZipLib.dll".Equals(filename))
{
string localFile = System.Environment.CurrentDirectory + @"\" + filename;
if (File.Exists(localFile))
{
File.Delete(localFile);
}
File.Copy(name, localFile);
File.Delete(name);
}
else
{
File.Delete(name);
}
}
}
catch (Exception ex)
{
Curtain.Log.Logger.Error(ex);
}
}
}
}
// 系统登录
Login(args);
if (_isLogin)
{
//#if DEBUG
// 如果升级版本发布错误,导致客户端都不能升级了。
// 主程序
//using (F_SYS_0201 main = new F_SYS_0201())
//{
// main.Text = _mainFormTitle;
// Application.Run(main);
//}
//#else
// 自动更新
NeedUpgradeResultEntity needUpgradeInfo = DKIBOSSPRDProxy.Service.IsNeedUpgrade(_info.FileVersion);
if (needUpgradeInfo.UpgradeState)
{
MessageBox.Show("检测到服务器上有最新版本,需要自动更新。",
Messages.MSG_TITLE_I01,
MessageBoxButtons.OK,
MessageBoxIcon.Information);
needUpgradeInfo.GradeInfo =
string.Format("本地程序版本:{0}\r\n最新程序版本:{1}",
_info.FileVersion, needUpgradeInfo.ServerVersion);
// 如果有提示信息提示,没有就跳过
if (!string.IsNullOrEmpty(needUpgradeInfo.GradeInfo))
{
F_SYS_0103 frmAutoUpgradeInfo = new F_SYS_0103(needUpgradeInfo.GradeInfo);
DialogResult dialogResult = frmAutoUpgradeInfo.ShowDialog();
if (dialogResult != DialogResult.OK)
{
Application.Exit();
}
else
{
Application.Exit();
//Process.Start("AutoUpgrade.exe");
Process.Start(System.AppDomain.CurrentDomain.BaseDirectory + "AutoUpgrade.exe", "IBOSSPRD.exe " + _frmLogin.Password);
}
}
else
{
Application.Exit();
//Process.Start("AutoUpgrade.exe");
Process.Start(System.AppDomain.CurrentDomain.BaseDirectory + "AutoUpgrade.exe", "IBOSSPRD.exe " + _frmLogin.Password);
}
}
// 如果有提示信息,不能连接服务器
else if (!string.IsNullOrEmpty(needUpgradeInfo.GradeInfo))
{
MessageBox.Show(needUpgradeInfo.GradeInfo,
Messages.MSG_TITLE_I01,
MessageBoxButtons.OK,
MessageBoxIcon.Warning);
Application.Exit();
}
else
{
// 主程序
using (F_SYS_0201 main = new F_SYS_0201())
{
main.Text = _mainFormTitle;
Application.Run(main);
}
}
//#endif
}
}
catch (Exception ex)
{
// 对异常进行共通处理
ExceptionManager.HandleEventException("Program.cs", "Main()", Messages.MSG_TITLE_E01, ex);
}
finally
{
if (_isLogin)
{
try
{
// 退出系统
DKIBOSSPRDProxy.Service.Logout(LogInUserInfo.CurrentUser.UserID);
}
catch { }
}
if (ibossMutex != null)
{
ibossMutex.Close();
}
}
}
else
{
IntPtr iptWndMain = Utility.FindWindow(null, _mainFormTitle);
if (iptWndMain == IntPtr.Zero)
{
IntPtr iptWndLogin = Utility.FindWindow(null, _loginFormTitle);
if (iptWndLogin != IntPtr.Zero)
{
Utility.WakeupWindow(iptWndLogin);
}
}
else
{
Utility.WakeupWindow(iptWndMain);
}
}
}
static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
//throw new NotImplementedException();
ExceptionManager.HandleEventException(
"Program.cs",
"Application_ThreadException()",
Messages.MSG_TITLE_E01,
e.Exception);
}
#region 私有方法
///
/// 调取系统登录页面进行登录验证
///
///
private static bool Login(string[] args)
{
_frmLogin = new F_SYS_0101();
_frmLogin.Text = _loginFormTitle;
if (args != null)
{
int argsLength = args.Length;
if (argsLength == 1)
{
if ("IsChangedUser" == args[0])
{
_frmLogin.IsChangedUser = true;
}
}
if (argsLength == 2)
{
if ("IBOSSPRD.exe" == args[0])
{
_frmLogin.Password = args[1];
}
}
}
DialogResult dialogResult = _frmLogin.ShowDialog();
if (dialogResult == DialogResult.OK)
{
return true;
}
return false;
}
#endregion
}
}