Patrick
Patrick

Reputation: 8300

Getting assembly name

C#'s exception class has a source property which is set to the name of the assembly by default.
Is there another way to get this exact string (without parsing a different string)?

I have tried the following:

catch(Exception e)
{
    string str = e.Source;         
    //"EPA" - what I want               
    str = System.Reflection.Assembly.GetExecutingAssembly().FullName;
    //"EPA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
    str = typeof(Program).FullName;
    //"EPA.Program"
    str = typeof(Program).Assembly.FullName;
    //"EPA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
    str = typeof(Program).Assembly.ToString();
    //"EPA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
    str = typeof(Program).AssemblyQualifiedName;
    //"EPA.Program, EPA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
}

Upvotes: 258

Views: 250581

Answers (6)

Rafi Henig
Rafi Henig

Reputation: 6414

When you do not know the assembly's location, but have its display name (e.g. ReactiveUI, Version=18.0.0.0, Culture=neutral, PublicKeyToken=null):

var assemblyName = "ReactiveUI, Version=18.0.0.0, Culture=neutral, PublicKeyToken=null"
new AssemblyName(assemblyName).Name // "ReactiveUI"

Upvotes: 2

kiran
kiran

Reputation: 1072

You can use the AssemblyName class to get the assembly name, provided you have the full name for the assembly:

AssemblyName.GetAssemblyName(Assembly.GetExecutingAssembly().Location).Name

or

AssemblyName.GetAssemblyName(e.Source).Name

MSDN Reference - AssemblyName Class

Upvotes: 11

ivan.ukr
ivan.ukr

Reputation: 3551

Assembly.GetExecutingAssembly().Location

Upvotes: 1

user6438653
user6438653

Reputation:

You could try this code which uses the System.Reflection.AssemblyTitleAttribute.Title property:

((AssemblyTitleAttribute)Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(), typeof(AssemblyTitleAttribute), false)).Title;

Upvotes: 3

Jim Lahman
Jim Lahman

Reputation: 2757

I use the Assembly to set the form's title as such:

private String BuildFormTitle()
{
    String AppName = System.Reflection.Assembly.GetEntryAssembly().GetName().Name;
    String FormTitle = String.Format("{0} {1} ({2})", 
                                     AppName, 
                                     Application.ProductName, 
                                     Application.ProductVersion);
    return FormTitle;
}

Upvotes: 10

Jaster
Jaster

Reputation: 8581

System.Reflection.Assembly.GetExecutingAssembly().GetName().Name

or

typeof(Program).Assembly.GetName().Name;

Upvotes: 474

Related Questions