chr
2025-01-03 31a636e735a0addc56e4f4527f500b7aa0874eb5
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);
            // 暂停一秒不然AlertKill不掉
            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 = "下载完成,解压中,请勿关闭程序...";
            // 临时解压目录,防止解压出错
            InitUnZipDir();
            // kill掉监听程序,避免文件占用
            KillListenerProcess();
            // 等待一秒释放exe占用
            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");
                }
                // 没有路径或者路径指向exe不对,就更新自启动路径
                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)