260

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

6 Answers 6

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

or

typeof(Program).Assembly.GetName().Name;
Sign up to request clarification or add additional context in comments.

4 Comments

VS show errors on resolve usings. You can use Assembly.GetEntryAssembly().GetName().Name;
Actually it should be typeof(any).GetTypeInfo().Assembly
@Thaina. I know this is an old comment, but could you explain why you should use GetTypeInfo().
@sgmoore At that time of my comment dotnet core 2.0 was still new and the Assembly property was existed in TypeInfo object in dotnet core 1.0, which is why GetTypeInfo() was still require, which I think mine is now an outdated practice
14

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

2 Comments

Just be glad you're not calling that from within an Office Addin - where GetEntryAssembly() will return null
In my case I need executing exe name rather than library dll name. This works very well
11

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

1 Comment

I got error because of parameter of GetAssemblyName method. I think it should have been Assembly.GetExecutingAssembly().Location instead of Assembly.GetExecutingAssembly().FullName.
3

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

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

Comments

2

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"

Comments

1

Assembly.GetExecutingAssembly().Location

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.