2025-07-16 18:08:40 +08:00
|
|
|
using JinYuan.ControlCenters;
|
|
|
|
|
using JinYuan.Helper;
|
|
|
|
|
using JinYuan.Models;
|
|
|
|
|
using JinYuan.VirtualDataLibrary;
|
2025-07-31 10:01:09 +08:00
|
|
|
using NLog;
|
|
|
|
|
using NLog.Config;
|
2025-07-16 18:08:40 +08:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Configuration;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
|
|
|
|
namespace LargeSquareOne
|
|
|
|
|
{
|
|
|
|
|
internal static class Program
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
#region 调用系统API
|
|
|
|
|
|
|
|
|
|
[DllImport("User32.dll")]
|
|
|
|
|
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
|
|
|
|
|
|
|
|
|
|
[DllImport("User32.dll")]
|
|
|
|
|
private static extern bool SetForegroundWindow(IntPtr hWnd);
|
|
|
|
|
|
|
|
|
|
private const int SW_MAXIMIZE = 3;
|
|
|
|
|
|
|
|
|
|
#endregion 调用系统API
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 应用程序的主入口点。
|
|
|
|
|
/// </summary>
|
|
|
|
|
[STAThread]
|
|
|
|
|
static void Main()
|
|
|
|
|
{
|
2025-07-31 10:01:09 +08:00
|
|
|
LoggerHelp.WriteInfo("Application 启动");
|
2025-07-16 18:08:40 +08:00
|
|
|
Application.EnableVisualStyles();
|
|
|
|
|
Application.SetCompatibleTextRenderingDefault(false);
|
|
|
|
|
string strname = Process.GetCurrentProcess().ProcessName;
|
|
|
|
|
int a = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Count();
|
|
|
|
|
if (Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Count() > 1)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("软件已经启动,请在后台确认。");
|
|
|
|
|
Thread.Sleep(1000);
|
|
|
|
|
Environment.Exit(0);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Process instance = GetRunningInstance();
|
|
|
|
|
if (instance == null)
|
|
|
|
|
{
|
|
|
|
|
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
|
|
|
|
|
ReadConfig();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (!CommonMethods.sysConfig.SwipeCardMode)
|
|
|
|
|
{
|
|
|
|
|
FrmLogin frm = new FrmLogin();
|
|
|
|
|
if (frm.ShowDialog() == DialogResult.OK)
|
|
|
|
|
{
|
|
|
|
|
Application.Run(new FrmMain());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Application.Run(new FrmMain());
|
|
|
|
|
}
|
2025-07-31 10:01:09 +08:00
|
|
|
|
2025-07-16 18:08:40 +08:00
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// 激活以前的实例
|
|
|
|
|
HandleRunningInstance(instance);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void ReadConfig()
|
|
|
|
|
{
|
|
|
|
|
IniConfigHelper.CreateIniFile(Application.StartupPath + "\\Config\\", "Configure.ini");
|
|
|
|
|
string connString = ConfigurationManager.ConnectionStrings["connString"].ToString();
|
|
|
|
|
string connString2 = ConfigurationManager.ConnectionStrings["connString2"].ToString();
|
|
|
|
|
string strDBType = ConfigurationManager.ConnectionStrings["connDbType"].ToString();
|
|
|
|
|
var connDbType = (SqlSugar.DbType)Enum.Parse(typeof(SqlSugar.DbType), strDBType);
|
|
|
|
|
CommonMethods.db.SetConnectionStr(connString, connDbType);
|
|
|
|
|
CommonMethods.dbServer.SetConnectionStr(connString2, connDbType);
|
2025-07-31 10:01:09 +08:00
|
|
|
CommonMethods.sysConfig.SwipeCardMode = Convert.ToBoolean(IniConfigHelper.ReadIniData("配置信息", "开启刷卡模式", "false"));
|
2025-07-16 18:08:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 当某个异常未被捕获时出现,回调方法
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Exception ex = (Exception)e.ExceptionObject;
|
|
|
|
|
|
2025-07-31 10:01:09 +08:00
|
|
|
LoggerHelp.WriteEX(ex);
|
2025-07-16 18:08:40 +08:00
|
|
|
MessageBox.Show($"程序出现大异常,请联系 【金源自动化软件设计部】。\r\n异常信息: {ex.Message}\r\n异常堆栈:{ex.StackTrace}",
|
|
|
|
|
"致命错误", MessageBoxButtons.OK, MessageBoxIcon.Stop);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
Application.Exit();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 返回正在运行的程序进程
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static Process GetRunningInstance()
|
|
|
|
|
{
|
|
|
|
|
Process current = Process.GetCurrentProcess();
|
|
|
|
|
Process[] processes = Process.GetProcessesByName(current.ProcessName);
|
|
|
|
|
|
|
|
|
|
foreach (Process process in processes)
|
|
|
|
|
{
|
|
|
|
|
if (process.Id != current.Id)
|
|
|
|
|
if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
|
|
|
|
|
return process;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 已经有了就把它激活,并将其窗口放置最前端
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="instance"></param>
|
|
|
|
|
public static void HandleRunningInstance(Process instance)
|
|
|
|
|
{
|
|
|
|
|
ShowWindowAsync(instance.MainWindowHandle, SW_MAXIMIZE);
|
|
|
|
|
SetForegroundWindow(instance.MainWindowHandle);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|