C#: Compare Application Version Numbers


UPDATE: As discussed in the comments, 99% of the time using the System.Version class included in the .net Framework will suffice for comparing application version numbers. Documentation for this can be found here. The below post will help you if for some reason (ex. you use commas instead of periods to separate the version number) the System.Version class will not work

When incorporating an automatic update feature into an application, one of the things that you will need to do is compare the installed version number to the currently available version number. Given version numbers of the format ‘X.X.X.X’ I have seen some people suggest simply removing the periods, converting the string to an integer and doing a simple comparison of the integers. Such a solution works great if you have complete control over the format of the version number and you can ensure the number of digits per grouping stays the same. But, suppose you have the following two version numbers: 1.4.0 and 1.1.28. Following the logic explained above, you will be comparing the integers 140 and 1128 and the second version will be determined to be greater than the first, which is incorrect.

In order to alleviate this problem, I have created the following method below to correctly compare two version numbers. This method uses the SplitToList extension which takes a string, splits it based on a given string and then converts each element to the specified type. You can find a more detailed explanation and the code here.

public static int CompareVersionNumbers(string versionA, string versionB)
{           
    // Convert each version numbers string to a list of integers
    List<int> a = versionA.SplitToList<int>(".");
    List<int> b = versionB.SplitToList<int>(".");            
   
    // Ensure that each of the lists are the same length
    while(a.Count < b.Count) { a.Add(0); }
    while(b.Count < a.Count) { b.Add(0); }

    // Compare elements of each list
    for (int i = 0; i < a.Count; i++)
    {
        // If the element from list A is greater than B, 
        // versionA is greater than versionB and visa versa.
        // If they are equal, go to the next element.
        if (a[i] > b[i])
            return 1;
        else if (b[i] > a[i])
            return -1;
    }

    // If we reach this point, the versions are equal
    return 0;
}
About these ads

4 Responses to “C#: Compare Application Version Numbers”

  1. Clinton Sheppard Says:

    I’d convert the strings to instances of System.Version and let the framework to the work.

    return new Version(versionA).CompareTo(new Version(versionB));

    For more complex version strings normalize first. see:

    http://stackoverflow.com/questions/30494/compare-version-identifiers

    • Nick Olsen Says:

      Great solution. I was not aware of the Version class. After some testing though, I did see some problems with the Version class. For instance you cannot pass in a version number that only has one grouping. For instance, comparing version 1 to version 2 will fail. You have to have at least 1.0 and 2.0. But as you said, some normalization might be required.

      Thanks for the info!

  2. Jacob Tabak Says:

    Good article BUT you would be doing everyone a favor by pointing them to the version class before you go into detail about your solution. You were the top search result on google for this, and your solution is excessively verbose and relies on all sorts of helper functions.

    • Nick Olsen Says:

      A very valid point. I have added a link to the System.Version class in the framework for those that find this article. Thanks


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 66 other followers

%d bloggers like this: