From 31a636e735a0addc56e4f4527f500b7aa0874eb5 Mon Sep 17 00:00:00 2001
From: chr <chr@mailcom>
Date: 星期五, 03 一月 2025 11:21:46 +0800
Subject: [PATCH] 改了一大堆;

---
 AutoUpdater/MainWindow.xaml.cs |  117 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 117 insertions(+), 0 deletions(-)

diff --git a/AutoUpdater/MainWindow.xaml.cs b/AutoUpdater/MainWindow.xaml.cs
index 90225ce..b4b590a 100644
--- a/AutoUpdater/MainWindow.xaml.cs
+++ b/AutoUpdater/MainWindow.xaml.cs
@@ -2,6 +2,7 @@
 using AutoUpdater.Util;
 using ICSharpCode.SharpZipLib.Zip;
 using log4net;
+using Microsoft.Win32;
 using System;
 using System.Collections.Generic;
 using System.Diagnostics;
@@ -91,6 +92,65 @@
                 GetCurrentVersion();
                 // 妫�鏌ユ彃浠剁殑鏇存柊
                 CheckPluginUpdate();
+            }
+        }
+
+        public void KillProcessByFullPath(string exePath)
+        {
+            string exeName = Path.GetFileNameWithoutExtension(exePath);
+            Process[] ps = Process.GetProcessesByName(exeName);
+            if (ps.Length > 0)
+            {
+                foreach (Process p in ps)
+                {
+                    if (new Uri(p.MainModule.FileName) == new Uri(exePath))
+                    {
+                        p.Kill();
+                    }
+                }
+            }
+        }
+
+        /// <summary>
+        /// 缁堟涓や釜Listener鐩稿叧杩涚▼
+        /// </summary>
+        public void KillListenerProcess()
+        {
+            string dir = Path.GetDirectoryName(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
+            string path1 = Path.Combine(dir, "Listener", "SolidWorksListener.exe");
+            string path2 = Path.Combine(dir, "Alert.exe");
+            KillProcessByFullPath(path1);
+            // 鏆傚仠涓�绉掍笉鐒禔lertKill涓嶆帀
+            Thread.Sleep(1000);
+            KillProcessByFullPath(path2);
+        }
+
+        /// <summary>
+        /// 鍚姩Listener鐩稿叧杩涚▼
+        /// </summary>
+        public void StartListenerProcess()
+        {
+            try
+            {
+                string dir = Path.GetDirectoryName(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
+                string path1 = Path.Combine(dir, "Listener", "SolidWorksListener.exe");
+                if (File.Exists(path1))
+                {
+                    ProcessStartInfo info = new ProcessStartInfo
+                    {
+                        FileName = path1,
+                        WorkingDirectory = Path.GetDirectoryName(path1)
+                    };
+
+                    new Process
+                    {
+                        StartInfo = info
+                    }.Start();
+                }
+            }
+            catch (Exception ex)
+            {
+                Logger.Error("Start Listener Process Error!", ex);
             }
         }
 
@@ -235,6 +295,11 @@
             label.Content = "涓嬭浇瀹屾垚锛岃В鍘嬩腑锛岃鍕垮叧闂▼搴�...";
             // 涓存椂瑙e帇鐩綍锛岄槻姝㈣В鍘嬪嚭閿�
             InitUnZipDir();
+            // kill鎺夌洃鍚▼搴忥紝閬垮厤鏂囦欢鍗犵敤
+            KillListenerProcess();
+            // 绛夊緟涓�绉掗噴鏀緀xe鍗犵敤
+            Thread.Sleep(1000);
+
             // 鎻掍欢鏂囦欢澶� 榛樿鍦� AutoUpdater.exe 鐨勪笂绾х洰褰曚腑
             string pluginDir = new DirectoryInfo(TempDir).Parent.Parent.FullName;
             using (ZipInputStream zipIn = new ZipInputStream(File.Open(localFilePath, FileMode.Open)))
@@ -272,8 +337,60 @@
             DoFinished();
         }
 
+        /// <summary>
+        /// 妫�鏌ヨ嚜鍚姩娉ㄥ唽琛�
+        /// </summary>
+        private void CheckAutoRunRegKey()
+        {
+            RegistryKey root = null;
+            RegistryKey key = null;
+            try
+            {
+                root = Registry.CurrentUser;
+                key = root.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
+                string name = "SolidWorksListener";
+                if (key == null)
+                {
+                    key = root.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
+                }
+                // 娌℃湁璺緞鎴栬�呰矾寰勬寚鍚慹xe涓嶅锛屽氨鏇存柊鑷惎鍔ㄨ矾寰�
+                string listenerPath = key.GetValue(name)?.ToString();
+                string rootDir = new DirectoryInfo(Assembly.GetExecutingAssembly().Location).Parent.Parent.FullName;
+                string newPath = Path.Combine(rootDir, "Listener", "SolidWorksListener.exe");
+                //listenerPath = listenerPath.Replace("\"", "");
+                if (string.IsNullOrEmpty(listenerPath) || new Uri(listenerPath) != new Uri(newPath))
+                {
+                    key.SetValue(name, newPath, RegistryValueKind.String);
+                }
+            }
+            catch (Exception e)
+            {
+                Logger.Error("CheckAutoRunRegKey Failed!", e);
+            }
+            finally
+            {
+                try
+                {
+                    if (key != null) key.Close();
+                    if (root != null) root.Close();
+                }
+                catch (Exception ex)
+                {
+                    Logger.Error("Reg Key Close Failed!", ex);
+                }
+            }
+        }
+
+        private void BeforeRestart()
+        {
+            CheckAutoRunRegKey();
+            // 鍚姩鐩戝惉杩涚▼
+            StartListenerProcess();
+        }
+
         private void DoFinished()
         {
+            BeforeRestart();
             label.Content = "鏇存柊瀹屾垚";
             bar.IsIndeterminate = false;
             if (SwAppPath != null)

--
Gitblit v1.9.1