我的第一次尝试是查询Win32_OperatingSystem的标题,并测试标题是否“等于”我正在测试的操作系统:
Dim objWMIService, strComputer
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
msgbox getOperatingSystemCaption()
msgbox meetsOperatingSystemRequirement()
Function getOperatingSystemCaption()
Dim strCaption, colOperatingSystems, objOperatingSystem
Set colOperatingSystems = objWMIService.ExecQuery _
("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
strCaption = objOperatingSystem.Caption
Exit For
Next
getOperatingSystemCaption = strCaption
End Function
Function meetsOperatingSystemRequirement()
meetsOperatingSystemRequirement = False
If getOperatingSystemCaption() = "Microsoft Windows 7 Home Premium" Then
meetsOperatingSystemRequirement = True
End If
End Function
我想我可以使用InStr,但是我仍然不明白为什么“Caption”和我的字符串不相等。
答案 0 :(得分:2)
您确定拥有“Microsoft Windows XP”而不是“Microsoft Windows XP Professional”吗?如果您使用“=”符号,那么您将无法捕获它,因为它希望匹配完全字符串。如果你想要部分匹配,使用instr()会更好。否则,请添加“专业”
找到字幕后可以进行一些调试
....
msgbox strCaption & " " & len(strCaption)
getOperatingSystemCaption = strCaption
....
尝试不同的方式
.....
myCaption = getOperatingSystemCaption()
msgbox myCaption & " " & len(myCaption)
If myCaption = "Microsoft Windows XP Premium Home" Then
......
检查长度......