天津投入产出系统后端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

205 lines
8.0 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using QMAPP.WinForm.Common;
using QMAPP.ServicesAgent.LoginService;
using System.Deployment.Application;
using System.Diagnostics;
namespace QMAPP.WinForm
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] Args)
{
bool createNew;
using (System.Threading.Mutex m = new System.Threading.Mutex(true, Application.ProductName, out createNew))
{
if (createNew)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//绑定异常处理事件
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
string path = System.Configuration.ConfigurationManager.AppSettings["SaveFilePath"];
if (!System.IO.Directory.Exists(path))
{
System.IO.Directory.CreateDirectory(path);
}
var switchserver = LocalSetting.Settings["SwitchServer", ""];
if (string.Equals(switchserver,"1"))
{
ServerSwitch frmSwitch = new ServerSwitch();
frmSwitch.ShowDialog();
LocalSetting.Settings["SwitchServer"] = "0";
}
//系统初始化
ClientContext.Init();
//加载静态基础数据
QMFrameWork.Common.Util.ModelDictionaryHandler.Configure();
Application.Run(new LoginForm());
}
else
{
MessageBox.Show("程序已启动!");
}
}
}
#region 异常处理事件
static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
HandleException(e.Exception);
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
try
{
if (e.ExceptionObject is System.Exception)
{
HandleException((System.Exception)e.ExceptionObject);
}
}
catch (Exception ex)
{
HandleException(ex);
}
}
#endregion
#region 处理异常
static void HandleException(Exception ex)
{
try
{
//AppService.RecordException(ex);
System.Text.StringBuilder error = new System.Text.StringBuilder();
error.AppendFormat("BEGIN:{0:yyyy-MM-dd HH:mm:ss}-------------\r\n", DateTime.Now);
error.AppendLine(ex.Message);
error.AppendLine(ex.StackTrace);
error.AppendLine("-----------------------------------------END");
WriteLog.WriteError(error.ToString());
}
catch
{ }
finally
{
if (ex.GetBaseException() is System.BadImageFormatException)
{
if (!ApplicationDeployment.IsNetworkDeployed)
{
MessageBox.Show("程序文件损坏!需要重新安装程序。", "程序异常", MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.ExitThread();
Environment.Exit(0);
}
else
{
var result=MessageBox.Show("程序文件损坏!需要重新安装程序。\r\n点击“确定”运行自动卸载。","程序异常",MessageBoxButtons.OKCancel,MessageBoxIcon.Error);
if (result == DialogResult.OK)
{
try
{
//var ad = ApplicationDeployment.CurrentDeployment;
//ApplicationDeployment.CurrentDeployment.
//System.IO.Directory.Delete(Application.StartupPath);
//MessageBox.Show("应用程序已卸载,现在将重新启动安装程序。","程序更新",MessageBoxButtons.OK,MessageBoxIcon.Information);
//Application.Restart();
var bat = CreateUninstallBat();
Process p = new Process();
var pinfo = new ProcessStartInfo(bat);
pinfo.WorkingDirectory = "C:\\";
Process.Start(pinfo);
}
catch (DeploymentDownloadException dde)
{
MessageBox.Show("未能更新应用程序! \n\n请检查网络连接是否正常, 或再次尝试。 \r\n 错误: " + dde,"程序更新",MessageBoxButtons.OK,MessageBoxIcon.Error);
Application.ExitThread();
Environment.Exit(0);
}
}
else
{
Application.ExitThread();
Environment.Exit(0);
}
}
}
else
{
MessageBox.Show(ex.Message, "程序异常", MessageBoxButtons.OK, MessageBoxIcon.Error);
//Application.Exit();
}
}
}
#endregion
#region 生成删除安装目录批处理命令
public static string CreateUninstallBat()
{
string batpath = System.Configuration.ConfigurationManager.AppSettings["SaveFilePath"];
batpath = batpath + "MESAutoUninstall.bat";
string exepath = Application.StartupPath;
string[] pathparts=exepath.Split(System.IO.Path.DirectorySeparatorChar);
string apppath = System.IO.Path.GetPathRoot(exepath)+System.IO.Path.Combine(pathparts.Skip(1).Take(pathparts.Length-3).ToArray());
System.Text.StringBuilder cmd = new System.Text.StringBuilder();
cmd.AppendLine("@echo off");
cmd.AppendLine("mode con cols=80 lines=20");
cmd.AppendLine("title MES自动卸载工具");
cmd.AppendLine("cls");
cmd.AppendLine("echo 正在关闭MES程序...");
cmd.AppendLine("taskkill /F /IM QMAPP.WinForm.exe ");
cmd.AppendLine("timeout /t 5 /nobreak");
cmd.AppendLine("cls");
cmd.AppendLine("echo 正在卸载MES程序...");
cmd.AppendLine(string.Format("rd/s/q \"{0}\"", apppath));
cmd.AppendLine("cls");
cmd.AppendLine("echo.");
cmd.AppendLine("echo MES程序卸载完成");
cmd.AppendLine("echo.");
cmd.AppendLine("echo 请重新启动MES程序");
cmd.AppendLine("timeout /t 5");
cmd.AppendLine(string.Format("del \"{0}\"", batpath));
cmd.AppendLine("exit");
using (var stream = System.IO.File.Create(batpath))
{
System.IO.StreamWriter sw = new System.IO.StreamWriter(stream, System.Text.Encoding.Default);
sw.Write(cmd.ToString());
sw.Close();
}
return batpath;
}
#endregion
}
}