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

---
 PdmSwPlugin.Main/SwAddIn/MainControl.cs |   82 ++++++++++++++++++++++++++++++++++++++++
 1 files changed, 81 insertions(+), 1 deletions(-)

diff --git a/PdmSwPlugin.Main/SwAddIn/MainControl.cs b/PdmSwPlugin.Main/SwAddIn/MainControl.cs
index 36758e6..6b1208c 100644
--- a/PdmSwPlugin.Main/SwAddIn/MainControl.cs
+++ b/PdmSwPlugin.Main/SwAddIn/MainControl.cs
@@ -17,6 +17,9 @@
 using PdmSwPlugin.MaterialSelect;
 using PdmSwPlugin.PriceCheck;
 using PdmSwPlugin.Common.Setting;
+using Microsoft.Win32;
+using System.IO;
+using System.Diagnostics;
 
 namespace PdmSwPlugin.Main
 {
@@ -28,7 +31,7 @@
     {
         public SldWorks SwApp { get; private set; }
 
-        private ILog Logger = LogManager.GetLogger("Main");
+        private ILog Logger;
         private PluginSetting GlobalConfig;
 
         private string[] plugins = {
@@ -55,11 +58,88 @@
 
         public MainControl(SldWorks swAddin)
         {
+            var log = LogManager.GetLogger("Main");
+            Logger = log;
             SwApp = swAddin;
             GlobalConfig = PluginSetting.Instance;
             InitializeComponent();
             self.DataContext = this;
             InitTcpServer();
+            try
+            {
+                CheckAutoRunRegKey();
+                StartListenerProcess();
+            }
+            catch (Exception e)
+            {
+                log.Error("Start Listener Error!", e);
+            }
+        }
+
+        /// <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.FullName;
+                string newPath = Path.Combine(rootDir, "Listener", "SolidWorksListener.exe");
+                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);
+                }
+            }
+        }
+
+        /// <summary>
+        /// 鍚姩Listener鐩稿叧杩涚▼
+        /// </summary>
+        public void StartListenerProcess()
+        {
+            string dir = 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();
+            }
         }
 
         // 鏈湴璋冭瘯鐢紝杩炴帴SolidWorks

--
Gitblit v1.9.1