This commit is contained in:
Akizon77
2025-08-28 12:26:30 +08:00
parent 423bf31985
commit 3453aecbec
2 changed files with 32 additions and 16 deletions

View File

@@ -0,0 +1,11 @@
package monitoring
import "testing"
func TestOsName(t *testing.T) {
if got := OSName(); got == "" {
t.Errorf("OSName() = %v, want non-empty string", got)
} else {
t.Logf("OSName() = %v", got)
}
}

View File

@@ -21,28 +21,33 @@ func OSName() string {
if err != nil { if err != nil {
return "Microsoft Windows" return "Microsoft Windows"
} }
// 如果是 Server 版本,直接返回原始名称
// Server 版本保持原样
if strings.Contains(productName, "Server") { if strings.Contains(productName, "Server") {
return productName return productName
} }
// Windows 11 // 如果注册表已经直接提供 Windows 11 名称,直接返回
majorVersion, _, err := key.GetIntegerValue("CurrentMajorVersionNumber") if strings.Contains(productName, "Windows 11") {
if err == nil && majorVersion >= 10 { return productName
buildNumberStr, _, err := key.GetStringValue("CurrentBuild") }
if err == nil {
buildNumber, err := strconv.Atoi(buildNumberStr) // Windows 11 从 build 22000 起。DisplayVersion 在 Win10 21H2 也会是 21H2不能作为判断依据。
if err == nil && buildNumber >= 22000 { // Windows 11 starts at build 22000 buildNumberStr, _, err := key.GetStringValue("CurrentBuild")
// Windows 11 Windows 10 Pro for Workstations if err == nil {
edition := strings.Replace(productName, "Windows 10 ", "", 1) if buildNumber, err2 := strconv.Atoi(buildNumberStr); err2 == nil && buildNumber >= 22000 {
// 旧字段可能仍然写着 Windows 10把前缀替换为 Windows 11
if strings.HasPrefix(productName, "Windows 10 ") {
edition := strings.TrimPrefix(productName, "Windows 10 ")
return "Windows 11 " + edition return "Windows 11 " + edition
} }
} if productName == "Windows 10" { // 极端精简情况
// DisplayVersion return "Windows 11"
displayVersion, _, err := key.GetStringValue("DisplayVersion") }
if err == nil && displayVersion >= "21H2" { // 如果不是以 Windows 10 开头,但 build 已经 >= 22000直接补成 Windows 11 + 原名称尾部
edition := strings.Replace(productName, "Windows 10 ", "", 1) if !strings.Contains(productName, "Windows 11") {
return "Windows 11 " + edition return strings.Replace(productName, "Windows 10", "Windows 11", 1)
}
} }
} }