By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,758 Members | 1,264 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,758 IT Pros & Developers. It's quick & easy.

Retrieve version information

P: n/a
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;
}

}
}

Aug 24 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Steve Barnett wrote:
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
Aug 24 '06 #2

P: n/a
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

"Markus Stoeger" <sp******@gmx.atwrote in message
news:u2**************@TK2MSFTNGP04.phx.gbl...
Steve Barnett wrote:
>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

Aug 25 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.