How to Use AssemblyInfo?

Hi,

The AssemblyInfo.cs file contains attributes, such as AssemblyVersion, which I want to use programatically, as in an About dialog. I have looked at the AssemblyVersionAttribute class, but I can't find any examples showing how to use it. Any help on this would be greatly appreciated.

Thanks,
Royce

Comments

  • : Hi,
    :
    : The AssemblyInfo.cs file contains attributes, such as AssemblyVersion, which I want to use programatically, as in an About dialog. I have looked at the AssemblyVersionAttribute class, but I can't find any examples showing how to use it. Any help on this would be greatly appreciated.
    :
    : Thanks,
    : Royce
    :
    :

    It looks like we have to use System.Reflection to solve this problem. The code is ugly, but here are two examples.

    First rerefence the Reflection namespace

    [code]
    using System.Reflection;
    [/code]

    Next try this code ...

    [code]
    // Get Current Assembly
    Assembly asm = Assembly.GetExecutingAssembly();

    // Get Title
    AssemblyTitleAttribute titleAtt =
    (AssemblyTitleAttribute)Attribute.GetCustomAttribute(asm,
    typeof(AssemblyTitleAttribute));

    // Get Description
    AssemblyDescriptionAttribute descAtt =
    (AssemblyDescriptionAttribute)Attribute.GetCustomAttribute(asm,
    typeof(AssemblyDescriptionAttribute));

    MessageBox.Show(titleAtt.Title);
    MessageBox.Show(descAtt.Description);

    // Version is easier, because you can use a built in property
    String version = asm.GetName().Version.ToString();
    MessageBox.Show(version);
    [/code]

    Another way to do it, is to loop through all the assembly attributes and load each value into a dictionary object ...

    First reference this namespace
    [code]
    using System.Collections.Specialized;
    [/code]

    Then build the dictionary like so ...

    [code]
    // Get Current Assembly
    Assembly asm = Assembly.GetExecutingAssembly();

    // Get ALL attributes from AssemblyInfo.cs
    Object[] atts = asm.GetCustomAttributes(false);
    StringDictionary asmValues = new StringDictionary();

    foreach(Object obj in atts)
    {
    //MessageBox.Show(obj.GetType().Name);
    switch(obj.GetType().Name)
    {
    case "AssemblyTitleAttribute":
    asmValues.Add("Title", ((AssemblyTitleAttribute)obj).Title);
    break;

    case "AssemblyDescriptionAttribute":
    asmValues.Add("Description", ((AssemblyDescriptionAttribute)obj).Description);
    break;

    }
    }

    // Read from the dictionary for now on
    MessageBox.Show(asmValues["Title"]);
    MessageBox.Show(asmValues["Description"]);
    [/code]

    Of course you have to add other case statements if you want the value of other attributes.

  • What iwilld0it posted will work like a charm and the same method can be used to examine any attribute, but if you're only after assembly version information, there is a more direct method.
    [code]
    AssemblyName assemName = Assembly.GetExecutingAssembly().GetName();
    string version = assemName.Version.ToString();
    [/code]

  • [b][red]This message was edited by darwen at 2004-12-15 9:56:13[/red][/b][hr]
    : Hi,
    :
    : The AssemblyInfo.cs file contains attributes, such as AssemblyVersion, which I want to use programatically, as in an About dialog. I have looked at the AssemblyVersionAttribute class, but I can't find any examples showing how to use it. Any help on this would be greatly appreciated.
    :
    : Thanks,
    : Royce
    :
    :

    If you're interested about just the settings on the executable you can gain access to many of the attributes just by using

    System.Windows.Forms.Application.

    e.g.

    System.Windows.Forms.Application.ProductName.

    It's a shortcut rather than having to go through the lengthy procedure of getting all the attributes attached to the executing assembly each time you want version information.

    Darwen.




Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories