chr
2024-10-09 1f645778ae80a3a8801b8bb4d0fcf8feb244ad43
AutoUpdater/MainWindow.xaml.cs
@@ -7,6 +7,7 @@
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows;
using VersionControl;
@@ -91,6 +92,58 @@
                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()
        {
            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();
            }
        }
@@ -235,6 +288,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)))
@@ -274,6 +332,8 @@
        private void DoFinished()
        {
            // 启动监听进程
            StartListenerProcess();
            label.Content = "更新完成";
            bar.IsIndeterminate = false;
            if (SwAppPath != null)