Is there a “standard” way to get a CLR Version class from a string?

I have recently become aware that our code has several implementations of a version parser (that is code to create a Version object from a version string). Thinking this was one of those times when people could not be bothered to read the docs I figured I would jump into MSDN, find the “standard” method to do this and replace the various methods with calls to that one. Much to my surprise the Version class does not have a Parse method, nor is there one on Convert or anywhere else that I searched.

Am I just missing it, or is there no method in the .Net API for converting a version string to a Version object. Please note I have the code to do it. In fact I have two different approaches that are being used in several different methods. What I am looking for is one in the standard library.

4 thoughts on “Is there a “standard” way to get a CLR Version class from a string?

  1. user

    This issue is that the Version string we get from our hardware is not a “standard” version string. It is of the form major.minor.patch (build), rather than major.minor.build.patch.

    Reply
  2. user

    Could you write an Extension method for a Version.Parse? That way it would be in one place.

    In C#…

    Version Parse(this Version ver, string Value) {
        // your conversion here.
    }
    

    In VB…

    Imports System.Runtime.CompilerServices
    
    Module VersionExtensions
    
        <Extension()> _
        Public Function Parse(ByVal aVersion As Version, ByVal aValue As String) As Version
            ' Conversion Here
        End Function
    
    End Module
    
    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *