Freigeben über


Version.TryParse Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Tries to convert the string representation of a version number to an equivalent Version object, and returns a value that indicates whether the conversion succeeded.

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

Syntax

'Declaration
Public Shared Function TryParse ( _
    input As String, _
    <OutAttribute> ByRef result As Version _
) As Boolean
public static bool TryParse(
    string input,
    out Version result
)

Parameters

  • input
    Type: System.String
    A string that contains a version number to convert.
  • result
    Type: System.Version%
    When this method returns, contains the Version equivalent of the number that is contained in input, if the conversion succeeded, or a Version object whose major and minor version numbers are 0 if the conversion failed.

Return Value

Type: System.Boolean
true if the input parameter was converted successfully; otherwise, false.

Remarks

The TryParse method is similar to the Parse method, except that it does not throw an exception if the conversion fails. Instead, it returns false if input is null, has fewer than two or more than four components, has at least one component that is not an integer, has at least one component that is less than zero, or has at least one component that is greater than Int32.MaxValue.

For the parse operation to succeed, the input parameter must be in 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 order, and must be separated by periods.

Examples

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

Module Example
   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim input As String = "4.0"
      ParseVersion(outputBlock, input)

      input = "4.0."
      ParseVersion(outputBlock, input)

      input = "1.1.2"
      ParseVersion(outputBlock, input)

      input = "1.1.2.01702"
      ParseVersion(outputBlock, input)

      input = "1.1.2.0702.119"
      ParseVersion(outputBlock, input)

      input = "1.3.5.2150000000"
      ParseVersion(outputBlock, input)
   End Sub

   Private Sub ParseVersion(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal input As String)
      Dim ver As Version = Nothing
      If Version.TryParse(input, ver) Then
         outputBlock.Text += String.Format("Converted '{0} to {1}.", input, ver) & vbCrLf
      Else
         outputBlock.Text += String.Format("Unable to determine the version from '{0}'.", _
                           input) & vbCrLf
      End If
   End Sub
End Module
' The example displays the following output:
'       Converted '4.0 to 4.0.
'       Unable to determine the version from '4.0.'.
'       Converted '1.1.2 to 1.1.2.
'       Converted '1.1.2.01702 to 1.1.2.1702.
'       Unable to determine the version from '1.1.2.0702.119'.
'       Unable to determine the version from '1.3.5.2150000000'.
using System;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      string input = "4.0";
      ParseVersion(outputBlock, input);

      input = "4.0.";
      ParseVersion(outputBlock, input);

      input = "1.1.2";
      ParseVersion(outputBlock, input);

      input = "1.1.2.01702";
      ParseVersion(outputBlock, input);

      input = "1.1.2.0702.119";
      ParseVersion(outputBlock, input);

      input = "1.3.5.2150000000";
      ParseVersion(outputBlock, input);
   }

   private static void ParseVersion(System.Windows.Controls.TextBlock outputBlock, string input)
   {
      Version ver = null;
      if (Version.TryParse(input, out ver))
         outputBlock.Text += String.Format("Converted '{0} to {1}.", input, ver) + "\n";
      else
         outputBlock.Text += String.Format("Unable to determine the version from '{0}'.",
                           input) + "\n";
   }
}
// The example displays the following output:
//       Converted '4.0 to 4.0.
//       Unable to determine the version from '4.0.'.
//       Converted '1.1.2 to 1.1.2.
//       Converted '1.1.2.01702 to 1.1.2.1702.
//       Unable to determine the version from '1.1.2.0702.119'.
//       Unable to determine the version from '1.3.5.2150000000'.

Version Information

Silverlight

Supported in: 5, 4

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.