PC Review


Reply
Thread Tools Rate Thread

AssemblyInfo.cs AssemblyVersion

 
 
Matthew Smith
Guest
Posts: n/a
 
      17th Feb 2005
Is there a way to get the value that is stored in the AssemblyVersion
attribute that's in AssemblyInfo.cs?
 
Reply With Quote
 
 
 
 
Derek Harmon
Guest
Posts: n/a
 
      17th Feb 2005
"Matthew Smith" <(E-Mail Removed)> wrote in message news:matty_d-(E-Mail Removed)...
> Is there a way to get the value that is stored in the AssemblyVersion
> attribute that's in AssemblyInfo.cs?


Yes, you can get a reference to the currently executing assembly and
then get it's custom attributes (filtering by type: AssemblyVersionAttribute)
like this,

using System.Reflection;
// . . .
Assembly assy = Assembly.GetExecutingAssembly( );
AssemblyVersionAttribute[] versionAttrs = (AssemblyVersionAttribute[ ])
assy.GetCustomAttributes( typeof( AssemblyVersionAttribute), false);
if ( versionAttrs != null && 0 < versionAttrs.Length ) {
Console.WriteLine( "Assembly version is {0}.", versionAttrs[ i].Version);
}

GetCustomAttributes( ) works for any Attribute but it returns an array of
plain object references, so don't forget to typecast it to the appropriate
Type of Attribute. If you pass the Type as the argument then you can be
guaranteed that the typecast is valid.


Derek Harmon


 
Reply With Quote
 
Matthew Smith
Guest
Posts: n/a
 
      17th Feb 2005
In article <#(E-Mail Removed)>,
"Derek Harmon" <(E-Mail Removed)> wrote:

> "Matthew Smith" <(E-Mail Removed)> wrote in message
> news:matty_d-(E-Mail Removed)...
> > Is there a way to get the value that is stored in the AssemblyVersion
> > attribute that's in AssemblyInfo.cs?

>
> Yes, you can get a reference to the currently executing assembly and
> then get it's custom attributes (filtering by type: AssemblyVersionAttribute)
> like this,
>
> using System.Reflection;
> // . . .
> Assembly assy = Assembly.GetExecutingAssembly( );
> AssemblyVersionAttribute[] versionAttrs = (AssemblyVersionAttribute[ ])
> assy.GetCustomAttributes( typeof( AssemblyVersionAttribute),
> false);
> if ( versionAttrs != null && 0 < versionAttrs.Length ) {
> Console.WriteLine( "Assembly version is {0}.", versionAttrs[
> i].Version);
> }
>
> GetCustomAttributes( ) works for any Attribute but it returns an array of
> plain object references, so don't forget to typecast it to the appropriate
> Type of Attribute. If you pass the Type as the argument then you can be
> guaranteed that the typecast is valid.


Of course I left off the qualifying phrase: "in .Net Compact Framework".
Grrr. GetCustomAttribute is not in the Compact Framework. Might just go
back to what I had originally, hard-coded value in C#.
 
Reply With Quote
 
Mattias Sjögren
Guest
Posts: n/a
 
      17th Feb 2005

>GetCustomAttributes( ) works for any Attribute


Except pseudo custom attributes such as AssemblyVersionAttribute. To
get the version you use assy.GetName().Version.



Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
 
Reply With Quote
 
Adam
Guest
Posts: n/a
 
      17th Feb 2005
Or a slightly simpler way:

string version = Assembly.GetExecutingAssembly().GetName().Version.ToString();

Adam
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Concerning AssemblyVersion =?Utf-8?B?UElFQkFMRA==?= Microsoft Dot NET 3 1st Feb 2006 04:02 PM
How to set/read AssemblyVersion in ASP.NET 2.0 =?Utf-8?B?TWFydGluIEJpc2Nob2Zm?= Microsoft ASP .NET 7 10th Oct 2005 05:33 PM
AssemblyVersion <> FileVersion Michael Maes Microsoft VB .NET 7 16th Aug 2004 11:20 AM
AssemblyVersion Mark Microsoft C# .NET 2 19th Apr 2004 07:43 PM
AssemblyInfo - AssemblyVersion (build and revision) Marcin Floryan Microsoft Dot NET 1 16th Mar 2004 09:59 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:15 PM.