NeoXUpdater.cs 1.7 KB
using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;

public static class AlgoUpdater
{
    /// <summary>
    /// 杀掉 algomatch.exe(若存在),然后将当前目录的 NeoX.dll 复制到 AlgoMatch\NeoX.dll。
    /// 复制失败则抛出异常。
    /// </summary>
    public static void UpdateNeoX()
    {
        // 1. 杀掉 algomatch.exe
        foreach (Process p in Process.GetProcessesByName("AlgoMatch"))
        {
            try
            {
                p.Kill();                 // 强制结束
                p.WaitForExit(5000);      // 最多等待 5 秒
            }
            catch (Exception ex)
            {
                // 若杀进程失败,可记录日志或继续(不影响复制)
                Console.WriteLine($"Warning: 结束进程失败: {ex.Message}");
            }
        }

        // 2. 构造路径
        string currentDir = AppDomain.CurrentDomain.BaseDirectory;
        string sourceFile = Path.Combine(currentDir, "NeoX.dll");
        string targetDir = Path.Combine(currentDir, "AlgoMatch");
        string targetFile = Path.Combine(targetDir, "NeoX.dll");

        // 3. 检查源文件
        if (!File.Exists(sourceFile))
        {
            MessageBox.Show("The source file NeoX.dll does not exist. \r\nFile:" + sourceFile);
            return;
        }

        // 4. 确保目标目录存在
        Directory.CreateDirectory(targetDir);

        // 5. 复制(可覆盖)
        try
        {
            File.Copy(sourceFile, targetFile, overwrite: true);
        }
        catch (Exception ex)
        {
            MessageBox.Show("Failed to copy NeoX.dll to the AlgoMatch directory. \r\nException:" + ex);
        }
    }
}