请问怎么准确地比较文件的版本号高低
比如某个软件的版本号是5.2.3790.24123,这里有几个点,不可能当成小数比较版本高低,但可能要把他和一个5.11.7883的版本号比较高低,逻辑上应该是后者高。
版本
[解决办法]
Option Explicit
Sub Main()
Debug.Print CompVer("5.2.3790.24123", "5.2.3790.24123")
Debug.Print CompVer("5.2.3790.24123", "5.2.3790.999")
Debug.Print CompVer("5.2.3790.24123", "5.11.7883")
End Sub
Function CompVer(ByVal v1 As String, ByVal v2 As String) As Long
Dim a1() As String
Dim a2() As String
Dim i As Long
a1 = Split(v1, ".")
a2 = Split(v2, ".")
For i = 0 To 3
Select Case Sgn(CInt(a1(i)) - CInt(a2(i)))
Case -1: GoTo LT
Case 0
Case 1: GoTo GT
End Select
Next
CompVer = 0
Exit Function
LT:
CompVer = -1
Exit Function
GT:
CompVer = 1
End Function
Dim strVer1 As String, strVer2 As String
Dim strItem1() As String, strItem2() As String
Dim i As Integer, n As Integer, d As Long
strVer1 = "5.2.3790.24123"
strVer2 = "5.11.7883"
strItem1 = Split(strVer1, ".")
strItem2 = Split(strVer2, ".")
n = UBound(strItem1)
If n < UBound(strItem2) Then n = UBound(strItem2)
For i = 0 To n
If strItem1(i) <> strItem2(i) Then Exit For
Next i
If i < n + 1 Then
d = CLng(strItem1(i)) - CLng(strItem2(i))
If d < 0 Then
MsgBox strVer1 & " < " & strVer2
Else
MsgBox strVer1 & " > " & strVer2
End If
Else
MsgBox strVer1 & " = " & strVer2
End If