简体   繁体   中英

How to check if version number is version between two version numbers

I am doing quite a not-too-crazy project related to TFS automation. Recently I had a discussion how and what is the most effective way to check if my version number (Major, Minor, HF, Build) - VersionToCheck has been released between versions SourceVersion and TargetVersion.

As a result, we have finished with very crazy alg:

  • cv - version to check
  • sv - source version
  • tv - target version

    if(c1.Major >= sv.Major and <= tv.Major) { //... check minor and rest parts } else { return false; }

As a result, we should get true/false in build number is between those two builds. To make it harder we don't have replicated numbers like 0001.0002.0003 so replace and comparing two ints won't work :)

Any tips are appreciated.

Ok to make more clear. For example I want to check if version 1.0.44.4736 is between versions 1.0.44.11 and 1.1.0.5.

The easiest way is to use the Version class - someone else has already written the logic for you.

var c1 = new Version(major, minor, build, revision);
var sv = new Version(major, minor, build, revision);
var tv = new Version(major, minor, build, revision);

if (c1 >= sv && c1 <= tv) ....

To take your specific example

Ok to make more clear. For example I want to check if version 1.0.44.4736 is between versions 1.0.44.11 and 1.1.0.5.

var c1 = new Version(1, 0, 44, 4736);
var sv = new Version(1, 0, 44, 11);
var tv = new Version(1, 1, 0, 5);
if (c1 >= sv && c1 <= tv)
{
    Console.WriteLine(c1 + " is between " + sv + " and " + tv); 
}

Run it here .


If you really want to implement this yourself...

public static int Compare(Version x, Version y)
{
    int result = x.Major.CompareTo(y.Major);
    if (result != 0)
        return result;
    result = x.Minor.CompareTo(y.Minor);
    if (result != 0)
        return result;
    result = x.Build.CompareTo(y.Build);
    if (result != 0)
        return result;
    result = x.Revision.CompareTo(y.Revision);
    return result;
}

Then

if (Compare(c1, sv) >= 0 && Compare(c1, tv) <= 0) ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM