Export (0) Print
Expand All

Version.Parse Method

Converts the string representation of a version number to an equivalent Version object.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

'Declaration
Public Shared Function Parse ( _
	input As String _
) As Version

Parameters

input
Type: System.String

A string that contains a version number to convert.

Return Value

Type: System.Version
An object that is equivalent to the version number specified in the input parameter.

ExceptionCondition
ArgumentNullException

input is Nothing.

ArgumentException

input has fewer than two or more than four version components.

ArgumentOutOfRangeException

At least one component in input is less than zero.

FormatException

At least one component in input is not an integer.

OverflowException

At least one component in input represents a number that is greater than Int32.MaxValue.

The input parameter must have the following format:

major.minor[.build[.revision]]

where major, minor, build, and revision are the string representations of the version number's four components: major version number, minor version number, build number, and revision number. Optional components are shown in square brackets ([ and ]). The components must appear in the specified order, and must be separated by periods.

Important noteImportant

Because the string representation of a version number must conform to a recognized pattern, applications should always use exception handling when calling the Parse method to parse user input. Alternatively, you can call the TryParse method to parse the string representation of a version number and return a value that indicates whether the parse operation succeeded.

The Parse method is a convenience method; it is equivalent to calling the Version(String) constructor.

The following example uses the Parse method to parse a number of strings that contain version information.

Module Example
   Public Sub Main()
      Dim input As String = "4.0"
      ParseVersion(input)

      input = "4.0."
      ParseVersion(input)

      input = "1.1.2"
      ParseVersion(input)

      input = "1.1.2.01702"
      ParseVersion(input)

      input = "1.1.2.0702.119"
      ParseVersion(input)

      input =  "1.3.5.2150000000"
      ParseVersion(input)
   End Sub 

   Private Sub ParseVersion(input As String)
      Try 
         Dim ver As Version = Version.Parse(input)
         Console.WriteLine("Converted '{0} to {1}.", input, ver)
      Catch e As ArgumentNullException
         Console.WriteLine("Error: String to be parsed is null.")
      Catch e As ArgumentOutOfRangeException
         Console.WriteLine("Error: Negative value in '{0}'.", input)
      Catch e As ArgumentException
         Console.WriteLine("Error: Bad number of components in '{0}'.", 
                           input)
      Catch e As FormatException
         Console.WriteLine("Error: Non-integer value in '{0}'.", input)
      Catch e As OverflowException   
         Console.WriteLine("Error: Number out of range in '{0}'.", input)                  
      End Try    
   End Sub 
End Module 
' The example displays the following output: 
'       Converted '4.0 to 4.0. 
'       Error: Non-integer value in '4.0.'. 
'       Converted '1.1.2 to 1.1.2. 
'       Converted '1.1.2.01702 to 1.1.2.1702. 
'       Error: Bad number of components in '1.1.2.0702.119'. 
'       Error: Number out of range in '1.3.5.2150000000'.

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4

.NET Framework Client Profile

Supported in: 4

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft