Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2006

Tip: Looking for answers? Try searching our database.

Retrieve version information

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Steve Barnett - 24 Aug 2006 13:19 GMT
I'm trying to put together a class that retrieves version information from
my assembly and am having a problem. The code below happily retrieves the
AssemblyTitle and AssemblyDescription but fails to retrieve the Version
number. Both of these are specified in the AssemblyInfo.cs file, so I can't
see why I can't get the version umber. Any suggestions?

Thanks
Steve

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

namespace Lucidus.IPM.IPM
{
   class VersionInformation
   {
       public static string ProductDescription()
       {
           Assembly currentAssembly = Assembly.GetExecutingAssembly();
           Type attrType = typeof(AssemblyDescriptionAttribute);

           object[] attrs = currentAssembly.GetCustomAttributes(attrType,
false);
           if (attrs.Length > 0)
           {
               AssemblyDescriptionAttribute desc =
(AssemblyDescriptionAttribute)attrs[0];
               return desc.Description;
           }
           else
           {
               return "";
           }
       }

       public static string ProductVersion()
       {
           Assembly currentAssembly = Assembly.GetExecutingAssembly();
           Type attrType = typeof(AssemblyVersionAttribute);

           // ***********************Always fails to get version
           object[] attrs = currentAssembly.GetCustomAttributes(attrType,
false);
           if (attrs.Length > 0)
           {
               AssemblyVersionAttribute desc =
(AssemblyVersionAttribute)attrs[0];
               return desc.Version;
           }
           else
           {
               return "0.0";
           }
       }

       public static string ProductName()
       {
           Assembly currentAssembly = Assembly.GetExecutingAssembly();
           Type attrType = typeof(AssemblyProductAttribute);

           object[] attrs = currentAssembly.GetCustomAttributes(attrType,
false);
           if (attrs.Length > 0)
           {
               AssemblyProductAttribute desc =
(AssemblyProductAttribute)attrs[0];
               return desc.Product;
           }
           else
           {
               return "";
           }
       }

       public static string ProductTitle()
       {
           Assembly currentAssembly = Assembly.GetExecutingAssembly();
           Type attrType = typeof(AssemblyTitleAttribute);

           object[] attrs = currentAssembly.GetCustomAttributes(attrType,
false);
           if (attrs.Length > 0)
           {
               AssemblyTitleAttribute desc =
(AssemblyTitleAttribute)attrs[0];
               return desc.Title;
           }
           else
           {
               return "";
           }
       }

       public static string ProductId()
       {
           Assembly currentAssembly = Assembly.GetExecutingAssembly();
           string prodId = ProductTitle() + " Version " + ProductVersion();

           System.Windows.Forms.MessageBox.Show(prodId);

           return prodId;
       }

   }
}
Markus Stoeger - 24 Aug 2006 19:01 GMT
> I'm trying to put together a class that retrieves version information from
> my assembly and am having a problem. The code below happily retrieves the
> AssemblyTitle and AssemblyDescription but fails to retrieve the Version
> number. Both of these are specified in the AssemblyInfo.cs file, so I can't
> see why I can't get the version umber. Any suggestions?

A long time ago, somewhere deep within the msdn docs, I read that the
AssemblyVersionAttribute is some kind of special attribute that you
cannot retrieve like other attributes. You have to use something like that:

public string ProductVersion() {
  return Assembly.GetExecutingAssembly().GetName().Version.ToString();
}

.. haven't tested it, but should work. As far as I know, that's the
easiest way to get at the version information specified in the
AssemblyVersion"Attribute".

hth,
Max
Steve Barnett - 25 Aug 2006 11:16 GMT
Just when I thought I was starting to get a handle on this stuff, they go
and throw me a curve-ball like this.

Worked perfectly, thanks.

Steve

>> I'm trying to put together a class that retrieves version information
>> from my assembly and am having a problem. The code below happily
[quoted text clipped - 16 lines]
> hth,
> Max

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.