Tuesday, October 30, 2012

Programmatically Get Assembly Version

In this tip I will discuss how we can get assembly version programmatically  from a caller application.  This can be done easily by taking help of Reflection.  Assembly  Class contains a static method called GetAssemblyFullName() which gets the assembly that contains the code that is currently executing.

FullName is a properties under Assembly Class which will return the full name of assembly the contains Assembly name, Version , Culture and Public Key Token.
Bellow is the code snippet for a sample assembly where I have created on Static Method Called GetAssemblyVersion.
  /// <summary>
    /// Class Test
    /// </summary>
    public class ClassTest
    {
        /// <summary>
        /// Classes the test.
        /// </summary>
        public ClassTest()
        {

        }

        /// <summary>
        /// Samples the method.
        /// </summary>
        /// <returns></returns>
        public static string SampleMethod()
        {
            return "Sample Method";
        }

        /// <summary>
        /// Gets the assembly version.
        /// </summary>
        /// <returns></returns>
        public string GetAssemblyVersion()
        {
            return Assembly.GetExecutingAssembly().FullName;
        }

    }
Add the created assembly as reference in a console application ( or any ) and call the GetAssemblyVersion() method as shown in below snippet.
ClassTest classTest = new ClassTest();
Console.WriteLine(classTest.GetAssemblyVersion());
You will get below output
image

No comments:

Post a Comment