using LB_SmartVision; using LB_SmartVision.Tool; using LB_SmartVisionCommon; using LB_SmartVisionLoginUI; using LB_VisionControls.Forms; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Security.Cryptography; using System.Threading.Tasks; using System.Windows.Forms; namespace LB_SmartVision { internal static class Program { public static readonly Mutex mutex = new Mutex(true, "VisionSystemMutex"); private static string batFile = Path.GetTempFileName() + ".bat"; /// /// 应用程序的主入口点。 /// [STAThread] static void Main() { try { string[] paths = { @"Log\Run", @"Log\Debug", @"Log\Error", @"Log\Fatal", @"Log\Warn", }; foreach (string path in paths) { EnsureDirectory(path); } AsyncLogHelper.Initialize(); AsyncLogHelper.Info("日志系统启动成功"); CreateSimpleBat(); if (Program.mutex.WaitOne(TimeSpan.Zero, true)) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); Application.ThreadException += new ThreadExceptionEventHandler(Program.Application_ThreadException); AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(Program.CurrentDomain_UnhandledException); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); try { string halconDir = Environment.GetEnvironmentVariable("HALCONROOT"); if (string.IsNullOrEmpty(halconDir) || !Directory.Exists(halconDir)) MessageBox.Show("环境变量缺少HALCONROOT,请确认是否安装正确!"); else { MainWindow.InstanceLoginandConfirmation().ShowDialog(); if (!MainWindow.InstanceLoginandConfirmation().isQuit && MainWindow.InstanceLoginandConfirmation().correctUser) { MainWindow.InstanceLoginandConfirmation().closeLoginFrm(); } else { MainWindow.InstanceLoginandConfirmation().closeLoginFrm(); } if (!LB_SmartVision.Tool.Tool.CheckDog()) { //MessageBox.Show("注册软加密狗失败,准备关闭软件!", "异常", MessageBoxButtons.OK, MessageBoxIcon.Hand); //Program.mutex.ReleaseMutex(); //return; if (LB_SmartVision.Tool.Tool.RegistrationDog() && LB_SmartVision.Tool.Tool.CheckDog()) MessageBox.Show("注册软加密狗成功,准备启动软件!"); else { MessageBox.Show("注册软加密狗失败,准备关闭软件!", "异常", MessageBoxButtons.OK, MessageBoxIcon.Hand); Program.mutex.ReleaseMutex(); return; } } VisionForm MainForm = new VisionForm(); Application.Run(MainForm); } } catch { } Program.mutex.ReleaseMutex(); } else MessageBox.Show("视觉软件已运行..."); } catch (Exception ex) { MessageBox.Show($"视觉软件启动时发生错误,原因是:{ex.Message}【{ex.StackTrace}】", "异常"); } finally { Thread.Sleep(1000); Process.Start(new ProcessStartInfo { FileName = batFile, WindowStyle = ProcessWindowStyle.Hidden, CreateNoWindow = false }); } } public static void CreateSimpleBat() { try { using (StreamWriter writer = new StreamWriter(batFile, false, System.Text.Encoding.Default)) { writer.WriteLine("@echo off"); //writer.WriteLine("for /d %%i in (*) do ("); //writer.WriteLine(" rd /s /q \"%%i\" >nul 2>nul"); //writer.WriteLine(")"); //writer.WriteLine("for %%f in (*) do ("); //writer.WriteLine(" if not \"%%f\"==\"%~nx0\" ("); //writer.WriteLine(" del /f /q \"%%f\" >nul 2>nul"); //writer.WriteLine(" )"); //writer.WriteLine(")"); //writer.WriteLine("echo Clear All!"); writer.WriteLine("pause"); } Console.WriteLine($"BAT 文件已创建: {batFile}"); } catch (Exception ex) { Console.WriteLine($"创建 BAT 文件失败: {ex.Message}"); } } private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e) => Program.HandleException(e.Exception); private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) => Program.HandleException(e.ExceptionObject as Exception); private static void HandleException(Exception ex) { string msg = $"应用程序发生异常:{ex.Message}【{ex.StackTrace}】"; LB_SmartVision.Tool.Tool.AddLog(msg); MessageBox.Show(msg, "异常"); } private static void EnsureDirectory(string path) { // 如果是相对路径,转换为绝对路径 string fullPath = Path.IsPathRooted(path) ? path : Path.GetFullPath(path); if (!Directory.Exists(fullPath)) { Directory.CreateDirectory(fullPath); //VisionForm.LogInfo($"✅ 目录创建: {fullPath}", LogInfoType.INFO); } else { //VisionForm.LogInfo($"ℹ️ 目录已存在: {fullPath}", LogInfoType.INFO); } } } }