1

How can I compare version string in the format of x.x.x.x

e.g. compare 3.0.1750 to 3.0.1749

meaning

if (3.0.1750 > 3.0.1749)
{
   // do this
}
Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
user829174
  • 6,132
  • 24
  • 75
  • 125

2 Answers2

8

Use the Version class to compare between versions:

var v1 = new Version("3.0.1750");
var v2 = new Version("3.0.1749");

bool isV1Greater = v1 > v2; // true
bool isV2Greater = v1 < v2; // false
Amir Popovich
  • 29,350
  • 9
  • 53
  • 99
6

You can use class Version to achieve that. Here is a sample of how to use it from MSDN:

Version v1 = new Version(2, 0);
Version v2 = new Version("2.1");
Console.Write("Version {0} is ", v1);
switch(v1.CompareTo(v2))
{
   case 0:
      Console.Write("the same as");
      break;
   case 1:
      Console.Write("later than");
      break;
   case -1:
      Console.Write("earlier than");
      break;
}
Console.WriteLine(" Version {0}.", v2);     
dotnetom
  • 24,551
  • 9
  • 51
  • 54