DevPinoy.org
A Filipino Developers Community
Attention: Site upgrade failed last night. Will resume updates this weekend. Thanks!- Keith

How To: Reading the Assembly Information In .NET

I've gotten this question 3 times in different flavors since last week and I've decided that it's time to post the solution in my blog. I've added comments on the code to explain what every line is doing.

The code below will demonstrate how you can read the assembly information in both C# and VB.NET.

[------------------ C# ------------------]

using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.IO;

namespace KeithRull.ReadingAssemblyInformation
{
    /// <summary>
    /// A sample program that demonstrates how to get the
    /// Assembly information. This is useful when you want
    /// to see the version of your class library.
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            //create a new assembly object that would signify
            //the current running assembly
            Assembly currentAssembly = Assembly.GetExecutingAssembly();

            //create the dictionary object that would contain
            //all the information about our assembly
            Dictionary<string, string> dictionaryOfProperties 
                        = GetAssemblyInfoDictionary(currentAssembly);

            //iterate thru each key in the dictionary
            foreach (string key in dictionaryOfProperties.Keys)
            {
                //print the key and its value
                Console.WriteLine("{0}: {1}", key, dictionaryOfProperties[key]);
            }

            //pause
            Console.ReadLine();

        }

        /// <summary>
        /// A method that returns a Dictionary of Assembly properties
        /// </summary>
        /// <param name="selectedAssembly">The assembly to inspect</param>
        /// <returns>The dictrionary containing the assembly information</returns>
        public static Dictionary<string, string> GetAssemblyInfoDictionary(Assembly selectedAssembly)
        {
            //Create the dictionary
            Dictionary<string, string> dictionaryOfProperties 
                                    = new Dictionary<string, string>();

            //get the location of the assembly
            string assemblyPath = selectedAssembly.Location;
            //add the assembly location to out dictionary
            dictionaryOfProperties.Add("AssemblyLocation", assemblyPath);

            //create the AssemblyName object based on our Assembly
            //this will enable us to get the version information
            //and other properties related to our assembly
            AssemblyName assemblyName = selectedAssembly.GetName();

            //add the FullName of out assembly
            dictionaryOfProperties.Add("AssemblyFullName", assemblyName.FullName);
            dictionaryOfProperties.Add("AssemblyName", assemblyName.FullName);

            //add the assembly version information
            dictionaryOfProperties.Add("Version", assemblyName.Version.ToString());
            dictionaryOfProperties.Add("Version.Major", assemblyName.Version.Major.ToString());
            dictionaryOfProperties.Add("Version.Minor", assemblyName.Version.Minor.ToString());
            dictionaryOfProperties.Add("Version.Build", assemblyName.Version.Build.ToString());
            dictionaryOfProperties.Add("Version.Revision", assemblyName.Version.Revision.ToString());
            
            //add the creation time
            DateTime creationTime = File.GetCreationTime(assemblyPath);
            dictionaryOfProperties.Add("CreationTime", creationTime.ToString());

            //add the last write time
            DateTime lastWriteTime = File.GetLastWriteTime(assemblyPath);
            dictionaryOfProperties.Add("LastWriteTime", creationTime.ToString());

            //return our dictionary obeject
            return dictionaryOfProperties;
        }
    }
}

[------------------ VB.NET ------------------]

Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Reflection
Imports System.IO

Namespace KeithRull.ReadingAssemblyInformation
    ''' <summary> 
    ''' A sample program that demonstrates how to get the 
    ''' Assembly information. This is useful when you want 
    ''' to see the version of your class library. 
    ''' </summary> 
    Module Program
        Sub Main(ByVal args As String())
            'create a new assembly object that would signify 
            'the current running assembly 
            Dim currentAssembly As Assembly = Assembly.GetExecutingAssembly()

            'create the dictionary object that would contain 
            'all the information about our assembly 
            Dim dictionaryOfProperties As Dictionary(Of String, String) = GetAssemblyInfoDictionary(currentAssembly)

            'iterate thru each key in the dictionary 
            For Each key As String In dictionaryOfProperties.Keys
                'print the key and its value 
                Console.WriteLine("{0}: {1}", key, dictionaryOfProperties(key))
            Next

            'pause 
            Console.ReadLine()

        End Sub

        ''' <summary> 
        ''' A method that returns a Dictionary of Assembly properties 
        ''' </summary> 
        ''' <param name="selectedAssembly">The assembly to inspect</param> 
        ''' <returns>The dictrionary containing the assembly information</returns> 
        Public Function GetAssemblyInfoDictionary(ByVal selectedAssembly As Assembly) As Dictionary(Of String, String)
            'Create the dictionary 
            Dim dictionaryOfProperties As New Dictionary(Of String, String)()

            'get the location of the assembly 
            Dim assemblyPath As String = selectedAssembly.Location
            'add the assembly location to out dictionary 
            dictionaryOfProperties.Add("AssemblyLocation", assemblyPath)

            'create the AssemblyName object based on our Assembly 
            'this will enable us to get the version information 
            'and other properties related to our assembly 
            Dim assemblyName As AssemblyName = selectedAssembly.GetName()

            'add the FullName of our assembly 
            dictionaryOfProperties.Add("AssemblyFullName", assemblyName.FullName)
            'add the Name of the assembly
            dictionaryOfProperties.Add("AssemblyName", assemblyName.Name)

            'add the assembly version information 
            dictionaryOfProperties.Add("Version", assemblyName.Version.ToString())
            dictionaryOfProperties.Add("Version.Major", assemblyName.Version.Major.ToString())
            dictionaryOfProperties.Add("Version.Minor", assemblyName.Version.Minor.ToString())
            dictionaryOfProperties.Add("Version.Build", assemblyName.Version.Build.ToString())
            dictionaryOfProperties.Add("Version.Revision", assemblyName.Version.Revision.ToString())

            'add the creation time 
            Dim creationTime As DateTime = File.GetCreationTime(assemblyPath)
            dictionaryOfProperties.Add("CreationTime", creationTime.ToString())

            'add the last write time 
            Dim lastWriteTime As DateTime = File.GetLastWriteTime(assemblyPath)
            dictionaryOfProperties.Add("LastWriteTime", creationTime.ToString())

            'return our dictionary obeject 
            Return dictionaryOfProperties
        End Function
    End Module
End Namespace

HTH ;)


Posted Feb 26 2008, 09:40 AM by keithrull

Add a Comment

(required)  
(optional)
(required)  
Remember Me?

Enter the numbers above:

Copyright DevPinoy 2005-2008
Powered by Community Server (Commercial Edition), by Telligent Systems