System.Version.TryParse 方法
方法描述
尝试将版本号的字符串表示形式转换为等效的 Version 对象,并返回一个指示转换是否成功的值。
语法定义(C# System.Version.TryParse 方法 的用法)
public static bool TryParse( string input, out Version result )
参数/返回值
| 参数值/返回值 | 参数类型/返回类型 | 参数描述/返回描述 | 
|---|---|---|
| input | System-String | 包含要转换的版本号的字符串。 | 
| result | System-Version% | 当此方法返回时,如果转换成功,则包含与 input 中所含编号等效的 Version;如果转换失败,则包含主版本号和次版本号都为 0 的 Version 对象。 | 
| 返回值 | System.Boolean | 如果 input 参数成功转换,则为 true;否则为 false。 | 
提示和注释
TryParse 方法类似于 Parse 方法,不同之处在于它在转换失败时不引发异常。 相反,如果 input 为 null,则它返回 false,它具有两个以下或四个以上组件、至少一个不为整数的组件、至少一个小于零的组件,或者至少具有一个大于 Int32.MaxValue 的组件。
为了分析操作成功进行,input 参数必须采用以下格式:
复制
major.minor[.build[.revision]]
其中,主、次、内部和修订分别是版本号的四个组件“主版本号、次版本号、内部版本号和修订号”的字符串表示形式。 可选组件显示在方括号([ 和 ])中: 这些组分必须按顺序显示,并且必须用句点分隔。
System.Version.TryParse 方法例子
下面的示例使用 TryParse 方法来解析一组包含版本信息的字符串。
using System;
public class Example
{
   public static void Main()
   {
      string input = "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);
   }
   private static void ParseVersion(string input)
   {
      Version ver = null;
      if (Version.TryParse(input, out ver))
         Console.WriteLine("Converted '{0} to {1}.", input, ver);
      else
         Console.WriteLine("Unable to determine the version from '{0}'.",
                           input);
   }
}
// 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'.
版本信息
.NET Framework 受以下版本支持:4 .NET Framework Client Profile 受以下版本支持:4
适用平台
Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。