From 6955655e64979f07e58cf455220c1f16f680ab0f Mon Sep 17 00:00:00 2001 From: zsyg <3872006562@qq.com> Date: Wed, 9 Jul 2025 11:34:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=98=BE=E7=A4=BA=E5=9B=BE?= =?UTF-8?q?=E6=A0=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tools/PathHelper.cs | 79 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 tools/PathHelper.cs diff --git a/tools/PathHelper.cs b/tools/PathHelper.cs new file mode 100644 index 0000000..ee49b34 --- /dev/null +++ b/tools/PathHelper.cs @@ -0,0 +1,79 @@ +using System; +using System.IO; +using System.Text; + +namespace AppStore +{ + public static class PathHelper + { + /// + /// 清理可执行文件路径,移除参数和引号 + /// + public static string CleanExecutablePath(string path) + { + if (string.IsNullOrWhiteSpace(path)) + return path; + + // 处理带引号的路径 + path = path.Trim().Trim('"'); + + // 找到第一个空格或参数符号 + int paramIndex = path.IndexOfAny(new[] { ' ', '-', '/' }); + if (paramIndex > 0) + { + // 检查空格前的部分是否是有效路径 + string potentialPath = path.Substring(0, paramIndex).Trim(); + if (File.Exists(potentialPath)) + { + return potentialPath; + } + + // 尝试提取带空格的路径 + if (path.Contains("\"")) + { + int startQuote = path.IndexOf('"'); + int endQuote = path.IndexOf('"', startQuote + 1); + if (endQuote > startQuote) + { + potentialPath = path.Substring(startQuote + 1, endQuote - startQuote - 1); + if (File.Exists(potentialPath)) + { + return potentialPath; + } + } + } + } + + // 如果路径包含.exe,尝试提取到.exe结尾 + int exeIndex = path.IndexOf(".exe", StringComparison.OrdinalIgnoreCase); + if (exeIndex > 0) + { + string potentialPath = path.Substring(0, exeIndex + 4); + if (File.Exists(potentialPath)) + { + return potentialPath; + } + } + + // 最后尝试直接返回路径 + return path; + } + + /// + /// 验证路径是否指向可执行文件 + /// + public static bool IsValidExecutablePath(string path) + { + try + { + string cleanPath = CleanExecutablePath(path); + return File.Exists(cleanPath) && + cleanPath.EndsWith(".exe", StringComparison.OrdinalIgnoreCase); + } + catch + { + return false; + } + } + } +}