Retrieve version information

S

Steve Barnett

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;
}

}
}
 
M

Markus Stoeger

Steve said:
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
 
S

Steve Barnett

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top