如何在VB6中比较2个版本

时间:2016-01-11 05:46:28

标签: vb6

我有2个版本,比如1.0.2016.1000和1.0.2015.2000,其中第一个是更高版本。

如何在vb6中进行这些比较?

如果两者相同,则应返回true而不进行任何循环。否则,它应该做一个最小的循环机制,如果第一个版本更大则返回true,如果更小则返回false。

5 个答案:

答案 0 :(得分:0)

我使用以下方法执行比较,它按预期工作。

StrComp(strFileName1,strFileName2,vbBinaryCompare)

答案 1 :(得分:0)

我会拆分小数点上的版本字符串来进行比较:

Public Function CompareFileVersion(sFileName1 As String, sFileName2 As String) As Integer

    '// Return values:
    '// -1 = File Version 2 is greater than File Version 1
    '//  0 = Versions are the same
    '//  1 = File version 1 is greater than File Version 2
    Dim iResult As Integer

    Dim sFileVersion1 As String
    Dim sFileVersion2 As String
    Dim arrFileVersion1() As String
    Dim arrFileVersion2() As String
    Dim lVersion1 As Long
    Dim lVersion2 As Long
    Dim fs

    '// Let's initialize our result with 0
    iResult = 0

    '// Use FileSystemObject to get the file version
    Set fs = CreateObject("Scripting.FileSystemObject")
    sFileVersion1 = fs.getfileversion(sFileName1)
    sFileVersion2 = fs.getfileversion(sFileName2)

    '// Split the two supplied file versions by the "." character
    arrFileVersion1() = Split(sFileVersion1, ".")
    arrFileVersion2() = Split(sFileVersion2, ".")

    For i = 0 To UBound(arrFileVersion1())
        '// Convert the string to a number so we don't get false positive ("2">"19")
        lVersion1 = CLng(arrFileVersion1(i))
        lVersion2 = CLng(arrFileVersion2(i))
        If lVersion1 > lVersion2 Then
            iResult = 1
        ElseIf lVersion1 < lVersion2 Then
            iResult = -1
        End If

        'If we have found that the result is not > or <, no need to proceed
        If iResult <> 0 Then Exit For

    Next

    If UBound(arrFileVersion2) > UBound(arrFileVersion1) _
      And arrFileVersion2(UBound(arrFileVersion2)) <> 0 Then iResult = -1

    CompareFileVersion = iResult

End Function

答案 2 :(得分:0)

如何致电:

CheckVersion("1.0.2016.1000", "1.0.2015.2000")

返回一个整数:

- 1 - 第二个参数/版本更大

0 - 两个参数/版本都相同

1 - 第一个参数/版本更大

该函数处理多个长度版本,例如将2.1.12.2进行比较,后者更大,并且足够聪明,知道2.1.0与{{1}相同}

2.1

答案 3 :(得分:0)

感谢大家的回复。我还有一个解决方案来实现这一目标。

Private Function CompareFileVersion(strFileName1 As String, strFileName2 As         String) As Boolean

'如果版本相同则返回True。     如果StrComp(strFileName1,strFileName2,vbTextCompare)= 0那么         CompareFileVersion = True         退出功能     结束如果

Dim strAryFileVersion1() As String
Dim strAryFileVersion2() As String
strAryFileVersion1() = Split(strFileName1, ".")
strAryFileVersion2() = Split(strFileName2, ".")

' Return False if number of version segments differ.
If UBound(strAryFileVersion1) <> UBound(strAryFileVersion2) Then
    CompareFileVersion = False
    Exit Function
End If

Dim i As Integer
For i = 0 To UBound(strAryFileVersion1())
    '' Return True if current version segment greater than expected version segment.
    If CLng(strAryFileVersion2(i)) > CLng(strAryFileVersion1(i)) Then
        CompareFileVersion = True
        Exit Function
    End If
    ' Return False if current version segment less than expected version segment.
    If CLng(strAryFileVersion2(i)) < CLng(strAryFileVersion1(i)) Then
        CompareFileVersion = False
        Exit Function
    End If
    ' Current/Expected version segments are the same, continue to the next segment.
Next

CompareFileVersion = True

结束功能

答案 4 :(得分:0)

我会做这样的事情:

拨打:

CompareVersions("1.0.2016.1000", "1.0.2015.2000")

使用任何数字

完美适合我

<强>声明:

Function SameLength(ByVal Ver1 As String, ByVal Ver2 As String) As String
    Dim diff As Integer, num2 As Integer
    diff = UBound(Split(Ver2, ".")) - UBound(Split(Ver1, "."))
    SameLength = Ver1 & String((diff + Abs(diff)) / 2, ".")
End Function

Function CompareVersions(Ver1 As String, Ver2 As String) As Boolean
    Ver1 = SameLength(Ver1, vel2)
    Ver2 = SameLength(Ver2, Ver1)
    If Ver1 = Ver2 Then CompareVersions = True: Exit Function
    Dim xSplit1() As String, xSplit2() As String
    xSplit1 = Split(Ver1, ".")
    xSplit2 = Split(Ver2, ".")
    For x = 0 To UBound(xSplit1)
        If Val(xSplit1(x)) > Val(xSplit2(x)) Then
            CompareVersions = True: Exit Function
        ElseIf Val(xSplit1(x)) < Val(xSplit2(x)) Then
            CompareVersions = False: Exit Function
        End If
    Next x
End Function
  • 如果第一个版本等于或大于第二个版本,则返回True
  • 如果第一个版本小于第二个版本,则返回False

<强>试验:

CompareVersions("1.0.2016.1000", "1.0.2015.2000")   'True
CompareVersions("1.0.2015.2000", "1.0.2015.2000")   'True
CompareVersions("1.2.3.4.5", "1.2.3.4.5.1")         'False
CompareVersions("1.2.3.4.5.1", "1.2.3.4.5")         'True
CompareVersions("9.9.9", "8.8.8.8.8.8")             'True

希望有所帮助:)