From ba81428f3469f97706fa24be71d7c2baf8950a64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BE=99=E8=85=BE=E7=8C=AB=E8=B7=83?= <1043137532@qq.com> Date: Mon, 22 Jun 2026 18:08:24 +0800 Subject: [PATCH] StringUtils.ParseVersionWithDefaults --- Shared/Utils/StringUtils.cs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/Shared/Utils/StringUtils.cs b/Shared/Utils/StringUtils.cs index 40e5d9a..8b3e909 100644 --- a/Shared/Utils/StringUtils.cs +++ b/Shared/Utils/StringUtils.cs @@ -194,14 +194,11 @@ public static class StringUtils { /// /// 将字符串解析为版本号。 - /// 允许缺少次要版本(如 "2" 代表 "2.0")。 + /// 允许缺少任意多个段,且会将缺失的段不为 0,而不是 -1。例如,"2" 会被解析为 2.0.0.0。 /// - public static Version ToVersion(string value) { - if (Version.TryParse(value.Contains('.') ? value : value + ".0", out var version)) { - return version; - } else { - throw new FormatException($"无法将字符串 \"{value}\" 解析为版本号"); - } + public static Version ParseVersionWithDefaults(string value) { + while (value.Count(c => c == '.') < 3) value += ".0"; + return Version.Parse(value); } } \ No newline at end of file