这是我很久以前写的用于比较版本的类的精简版本。
Public Class Version
Implements IEquatable(Of Version)
Private _mmajorVersion As Integer = 0
Private _mminorVersion As Integer = 0
Private _mrivision As Integer = 0
Private _mbuild As Integer = 0
Sub New()
_mmajorVersion = 0
_mminorVersion = 0
_mrivision = 0
_mbuild = 0
End Sub
Sub New(ByVal VersionObj As Object)
If VersionObj Is DBNull.Value Then
_mmajorVersion = 0
_mminorVersion = 0
_mrivision = 0
_mbuild = 0
Else
ParseVersionString(VersionObj.ToString)
End If
End Sub
Public Sub New(ByVal VersionString As String)
ParseVersionString(VersionString)
End Sub
Public Property MajorVersion As Integer
Get
Return _mmajorVersion
End Get
Set(ByVal value As Integer)
_mmajorVersion = value
End Set
End Property
Public Property MinorVersion As Integer
Get
Return _mminorVersion
End Get
Set(ByVal value As Integer)
_mminorVersion = value
End Set
End Property
Public Property Revision As Integer
Get
Return _mrivision
End Get
Set(ByVal value As Integer)
_mrivision = value
End Set
End Property
Private Sub ParseVersionString(ByVal Version_String As String)
Dim p() As String = Version_String.Split(New Char() {"."}, 4)
If IsNumeric(p(0)) Then
_mmajorVersion = CInt(p(0))
End If
If p.Length > 1 Then
If IsNumeric(p(1)) Then
_mminorVersion = CInt(p(1))
End If
End If
If p.Length > 2 Then
If IsNumeric(p(2)) Then
_mrivision = CInt(p(2))
End If
End If
If p.Length > 3 Then
If IsNumeric(p(3)) Then
_mbuild = CInt(p(3))
End If
End If
End Sub
Public Shared Operator <(ByVal Version1 As Version, ByVal Version2 As Version) As Boolean
If Version1 = Version2 Then Return False
If Version1._mmajorVersion < Version2._mmajorVersion Then Return True
If Version1._mmajorVersion > Version2._mmajorVersion Then Return False
If Version1._mminorVersion < Version2._mminorVersion Then Return True
If Version1._mminorVersion > Version2._mminorVersion Then Return False
If Version1._mrivision < Version2._mrivision Then Return True
If Version1._mrivision > Version2._mrivision Then Return False
If Version1._mbuild < Version2._mbuild Then Return True
If Version1._mbuild > Version2._mbuild Then Return False
Return False
End Operator
Public Shared Operator >(ByVal Version1 As Version, ByVal Version2 As Version) As Boolean
Return Not (Version1 < Version2 Or Version1 = Version2)
End Operator
Public Shared Operator <=(ByVal Version1 As Version, ByVal Version2 As Version) As Boolean
If Version1 > Version2 Then Return False
Return True
End Operator
Public Shared Operator >=(ByVal Version1 As Version, ByVal Version2 As Version) As Boolean
If Version1 < Version2 Then Return False
Return True
End Operator
Public Shared Operator =(ByVal Version1 As Version, ByVal Version2 As Version) As Boolean
If Version1._mmajorVersion = Version2._mmajorVersion And _
Version1._mminorVersion = Version2._mminorVersion And _
Version1._mrivision = Version2._mrivision And _
Version1._mbuild = Version2._mbuild Then
Return True
End If
Return False
End Operator
Public Shared Operator <>(ByVal Version1 As Version, ByVal Version2 As Version) As Boolean
Return Not Version1 = Version2
End Operator
Public Overrides Function Equals(ByVal obj As Object) As Boolean
If obj Is Nothing Then Return False
If obj.GetType.Name <> Me.GetType.Name Then Return False
Dim v As Version = DirectCast(obj, Version)
Return Me = v
End Function
Public Overloads Function Equals(ByVal other As Version) As Boolean Implements System.IEquatable(Of Version).Equals
Return Me = other
End Function
Public Overrides Function ToString() As String
Return String.Format("{0}.{1}.{2}.{3}", _mmajorVersion.ToString, _mminorVersion.ToString, _mrivision.ToString, _mbuild.ToString)
End Function
Public Overrides Function GetHashCode() As Integer
Return Me.ToString.GetHashCode()
End Function
End Class
你可以这样调用类。
Return New Version(ApplicationVersionInformationArray(0)) > New Version("1.0.0.0")