Archive

Posts Tagged ‘Assembly version’

Version checker

December 30, 2010 Leave a comment

We often found code where we are trying to compare version of assembly or OS. Most of the time it is done wrong way with string comparison or string conversion to number etc. The correct way is to use the Version Class of .NET.

Retrieving the current application’s version:


<code>// Get the version of the executing assembly (that is, this assembly).
Assembly assem = Assembly.GetEntryAssembly();
AssemblyName assemName = assem.GetName();
Version ver = assemName.Version;
Console.WriteLine("Application {0}, Version {1}", assemName.Name, ver.ToString());</code>

Here is a code to compare two version:
<code>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);
// The example displays the following output:
// Version 2.0 is earlier than Version 2.1.</code>

<code>

Read more about it on MSDN.

Follow

Get every new post delivered to your Inbox.