From c4a3f8db90530a727ffab77c3bb5bb7d782792bf Mon Sep 17 00:00:00 2001
From: alielie <alielie@git.com>
Date: 星期一, 25 八月 2025 15:50:27 +0800
Subject: [PATCH] 上传代码;

---
 WPFProject/App.xaml.cs |   46 ++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 44 insertions(+), 2 deletions(-)

diff --git a/WPFProject/App.xaml.cs b/WPFProject/App.xaml.cs
index bbef0c0..c63a2e2 100644
--- a/WPFProject/App.xaml.cs
+++ b/WPFProject/App.xaml.cs
@@ -5,7 +5,9 @@
 using Microsoft.Win32;
 using Panuon.WPF.UI;
 using System;
+using System.Diagnostics;
 using System.IO;
+using System.Management;
 using System.Reflection;
 using System.Threading;
 using System.Windows;
@@ -35,6 +37,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 +66,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 +77,34 @@
             }
         }
 
+        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)
         {
+            var process = GetProcess();
+
             mutex = new Mutex(true, "TestPlatform_OnlyRun_CRNS");
-            if (mutex.WaitOne(0, false))
+            if (mutex.WaitOne(0, false))//&& process == null
             {
                 WriteRegister();
+                try
+                {
+                    DoUpdateAction();
+                }
+                catch (Exception ex) {
+                    log.Error("Update Failed.", ex);
+                }
+
                 logCleanner = new LogCleanner();
                 AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
                 LoggerHelper.Register(new CommonLogger());

--
Gitblit v1.9.1