chr
2024-11-02 b5234c5ab1e9e6826b8d8fc1e95fa752aaa40b74
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");
                }
                // 没有路径或者路径指向exe不对,就更新自启动路径
                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