From b5234c5ab1e9e6826b8d8fc1e95fa752aaa40b74 Mon Sep 17 00:00:00 2001
From: chr <chr@mailcom>
Date: 星期六, 02 十一月 2024 10:56:50 +0800
Subject: [PATCH] 优化程序

---
 AutoUpdater/MainWindow.xaml.cs |   83 +++++++++++++++++++++++++++++++++++------
 1 files changed, 70 insertions(+), 13 deletions(-)

diff --git a/AutoUpdater/MainWindow.xaml.cs b/AutoUpdater/MainWindow.xaml.cs
index 76d46fa..b4b590a 100644
--- a/AutoUpdater/MainWindow.xaml.cs
+++ b/AutoUpdater/MainWindow.xaml.cs
@@ -2,12 +2,12 @@
 using AutoUpdater.Util;
 using ICSharpCode.SharpZipLib.Zip;
 using log4net;
+using Microsoft.Win32;
 using System;
 using System.Collections.Generic;
 using System.Diagnostics;
 using System.IO;
 using System.Reflection;
-using System.Runtime.InteropServices;
 using System.Threading;
 using System.Windows;
 using VersionControl;
@@ -130,20 +130,27 @@
         /// </summary>
         public void StartListenerProcess()
         {
-            string dir = Path.GetDirectoryName(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
-            string path1 = Path.Combine(dir, "Listener", "SolidWorksListener.exe");
-            if (File.Exists(path1))
+            try
             {
-                ProcessStartInfo info = new ProcessStartInfo
+                string dir = Path.GetDirectoryName(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
+                string path1 = Path.Combine(dir, "Listener", "SolidWorksListener.exe");
+                if (File.Exists(path1))
                 {
-                    FileName = path1,
-                    WorkingDirectory = Path.GetDirectoryName(path1)
-                };
+                    ProcessStartInfo info = new ProcessStartInfo
+                    {
+                        FileName = path1,
+                        WorkingDirectory = Path.GetDirectoryName(path1)
+                    };
 
-                new Process
-                {
-                    StartInfo = info
-                }.Start();
+                    new Process
+                    {
+                        StartInfo = info
+                    }.Start();
+                }
+            }
+            catch (Exception ex)
+            {
+                Logger.Error("Start Listener Process Error!", ex);
             }
         }
 
@@ -330,10 +337,60 @@
             DoFinished();
         }
 
-        private void 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