代码改变世界

How to get all the information required by "AboutDialog".

2012-08-08 15:09  craystall  阅读(177)  评论(0)    收藏  举报

Usually, while designing a desktop application, we need a "AboutDialog" to show the product information, such as "Product Name", "Version" etc. Here are the code about it:

 

1. Get Assembly Title 

View Code
 1         private string AssemblyTitle
 2         {
 3             get
 4             {
 5                 object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
 6                 if (attributes.Length > 0)
 7                 {
 8                     AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0];
 9                     if (titleAttribute.Title != "")
10                     {
11                         return titleAttribute.Title;
12                     }
13                 }
14                 return System.IO.Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase);
15             }
16         }

 

2. Get Assembly Version

View Code
        private static string GetProductVersion()
        {
            string version = String.Empty;

            object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyInformationalVersionAttribute), false);
            if (attributes.Length == 0)
            {
                //just in case fall back to the regular dll version which is always there.
                version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
            }
            else
            {
                version = ((AssemblyInformationalVersionAttribute)attributes[0]).InformationalVersion;
            }

            return version;
        }

 

3. Get Development Company

View Code
        private string AssemblyCompany
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyCompanyAttribute)attributes[0]).Company;
            }
        }


4. Get Product Information

View Code
        private string AssemblyProduct
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyProductAttribute)attributes[0]).Product;
            }
        }