Version comparison for update method

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HaLo2FrEeEk
    Contributor
    • Feb 2007
    • 404

    Version comparison for update method

    I've written an update method for one of my programs and for the most part it works well, but there is a slight issue. The way I have it check for updates is to simply download an XML file from my server and check the major, minor, and revision numbers in the file against those of the application. So if my application version is 2.2.1 and the XML file says 2.2.2, then the program would know an update was available. Here is my code:

    Code:
    if (updateMajor > major || updateMinor > minor || updateRevision > revision)
    {
        // Update stuff
    }
    The problem is, it doesn't always work how it's supposed to. For example, if I have an application version 2.2.2 and the update file says 2.1.2, it will prompt me to update since the minor version number in the XML is smaller than te application's version.

    My question: what would be a better way to check for this? I keep going back to thinking nested if statements, something like:

    Code:
    if (updateMajor >= major)
    {
        if (updateMinor >= minor)
        {
            if (updateRevision > revision)
            {
                // Update stuff here...
            }
        }
    }
    But something seems wron with that. Maybe it's just me though, does this look right to you guys, or is there a better way?

    And for the record, I'm discarding the *actual* revision number in a standard Assembly Version, and substituting it with the Build number. I do this just because I think 4-digit versions are ugly. Personal preference.
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    The nested if statement is the same as changing all the || to && in your original one, and it would mean that 2.2.2 -> 2.2.3 wouldn't update.

    I wonder, could you just combine them all into a single number? Lets say you're reserving 3 digits for major and minor, then 4 digits for the revision... I think that's fairly common.

    You can just shift them into a double and compare the numbers.

    Code:
    double verNum = major * Path.Pow(10.0, 3.0) + minor + rev / Math.Pow(10.0, 4.0);
    So for your cases of 2.2.2 and 2.2.1, they should come out to 2002.0002 and 2002.0001 respectively. You can easily perform a comparison on those.

    Does that work?

    Comment

    Working...