Paket Merdeka Project::Getting Version from Your Application

When I create a software, I need users know what my application version. The short solution is to write “hardcode” version in your application but it’s a bad solution. In this article I want to show you how to display our application version by runtime. For example my application, M-Server (Messaging Server for SMS and Email). Here is M-Server Manager:

Getting Application Version
Currently .NET Framework has a solution to get application version but we do wrapping on these .NET classes. The simple solution is to get version from assembly so you should get assembly object. From that you can get current version using GetName().Version.ToString(). Now I create a wrapper class that you can use to get application version (Desktop or Web application) i.e.: DmcVersion. This class returns AssemblyInformation object defines here

public class AssemblyInformation
{
        private string _name;
        private string _ver;
        private string _fullName;
 

        public string Name
        {
            set
            {

                this._name = value;

            }
            get
            {

                return this._name;

            }

        }
        public string Version
        {
            set
            {

                this._ver = value;

            }
            get
            {

                return this._ver;

            }
        }
        public string FullName
        {
            set
            {

                this._fullName = value;

            }
            get
            {

                return this._fullName;

            }
        }

On DmcVersion object has a overloading method, GetVersion. You can pass a parameter such as file name or Assembly object

public bool GetVersion(Assembly asm)
{
            if (asm != null)
            {
                this._info = new AssemblyInformation();
                this._info.Name = asm.GetName().Name;
                this._info.Version = asm.GetName().Version.ToString();
                this._info.FullName = asm.GetName().ToString();
            }
            else
            {
                this._errMsg = "Invalid assembly";
                return false;
            }


            return GetReferenceAssembly(asm);
}

To get reference assembly, we can use GetReferencedAssemblies() method from Assembly object that returns AssemblyName collection objects

private bool GetReferenceAssembly(Assembly asm)
{
            try
            {
                AssemblyName[] list = asm.GetReferencedAssemblies();
                if (list.Length > 0)
                {
                    AssemblyInformation info = null;
                    _lstReferences = new List<AssemblyInformation>();
                    for (int i = 0; i < list.Length; i++)
                    {
                        info = new AssemblyInformation();
                        info.Name = list[i].Name;
                        info.Version = list[i].Version.ToString();
                        info.FullName = list[i].ToString();
 

                        this._lstReferences.Add(info);
                    }
                }
            }
            catch (Exception err)
            {
                this._errMsg = err.Message;
                return false;
            }
 

            return true;
}

Implementation
You can use easily both desktop and web application. For example

DmcVersion ver = new DmcVersion();
if (ver.GetVersion(Assembly.GetExecutingAssembly()))
{

     AssemblyInformation info = ver.CurrentAssemblyInfo;
     this.propertyGrid1.SelectedObject = info;
 

     List<AssemblyInformation> lst = ver.ReferenceAssembly;
     this.dataGridView1.DataSource = lst;
}

Here are samples for Desktop and web application

So where are the source sources for this paper ? yeaah...you download source code (C# 2.0) on http://workspaces.gotdotnet.com/paketmerdeka

Share this post: | | | |
Published Tuesday, May 23, 2006 7:34 PM by Agus Kurniawan
Filed under: ,

Comments

# re: Paket Merdeka Project::Getting Version from Your Application

Monday, August 11, 2008 3:08 PM by oikos

wah bagus yach artikelnya...

boleh minta samplenya ga...?

nyobain link sample yang diatas malah kemana2 ga jelas...

kalo boleh dan bisa minta dikirim via email aja...

ke crazy_aja80@yahoo.com

thx b4

nice artikel...

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above:
Powered by Community Server (Commercial Edition), by Telligent Systems