wenxiangchao
2026-11-02 9d1660270272180cd71147d6ddd53d1382643d2e
WPFProject/App.xaml.cs
@@ -3,9 +3,13 @@
using HMI.UI.Util;
using log4net;
using Microsoft.Win32;
using OpenTap.Addin.Util;
using Panuon.WPF.UI;
using Plugin.TsSupport;
using System;
using System.Diagnostics;
using System.IO;
using System.Management;
using System.Reflection;
using System.Threading;
using System.Windows;
@@ -35,6 +39,25 @@
                    MessageBoxIcon.Error, DefaultButton.YesOK, 5);
        }
        public static Process GetProcess()
        {
            Process cur = Process.GetCurrentProcess();
            string exePath = Assembly.GetExecutingAssembly().Location;
            string query = $"SELECT ProcessId FROM Win32_Process WHERE ExecutablePath = '{exePath.Replace("\\", "\\\\")}'";
            using (var searcher = new ManagementObjectSearcher(query))
            {
                foreach (var obj in searcher.Get())
                {
                    int processId = Convert.ToInt32(obj["ProcessId"]);
                    if (processId != cur.Id)
                    {
                        return Process.GetProcessById(processId);
                    }
                }
            }
            return null;
        }
        /// <summary>
        /// 启动后往注册表写点东西
        /// </summary>
@@ -45,7 +68,7 @@
            {
                string registryPath = @$"SOFTWARE\{Company}\{Program}";
                using RegistryKey key = Registry.LocalMachine.CreateSubKey(registryPath, true)
                using RegistryKey key = Registry.CurrentUser.CreateSubKey(registryPath, true)
                    ?? throw new Exception("无法创建或打开注册表键");
                string path = AppDomain.CurrentDomain.BaseDirectory;
                key.SetValue("Location", path, RegistryValueKind.String);
@@ -56,13 +79,35 @@
            }
        }
        private void DoUpdateAction()
        {
            using (SqliteHelper helper = new SqliteHelper(PathHelper.DatabasePath))
            {
                if (!helper.HasData("SELECT 1 FROM pragma_table_info('user') WHERE name = 'code';"))
                {
                    helper.ExecuteSql("ALTER TABLE user ADD code TEXT;");
                }
            }
        }
        private static Mutex mutex;
        private void Application_Startup(object sender, StartupEventArgs e)
        {
            mutex = new Mutex(true, "TestPlatform_OnlyRun_CRNS");
            if (mutex.WaitOne(0, false))
            var process = GetProcess();
            mutex = new Mutex(true, "TestPlatform_OnlyRun_CRNS__");
            if (mutex.WaitOne(0, false))//&& process == null
            {
                RegUtil.Register(new Net47Getter());
                WriteRegister();
                try
                {
                    DoUpdateAction();
                }
                catch (Exception ex) {
                    log.Error("Update Failed.", ex);
                }
                logCleanner = new LogCleanner();
                AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
                LoggerHelper.Register(new CommonLogger());