249

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"
}
Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
Patrick
  • 8,175
  • 7
  • 56
  • 72

6 Answers6

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

or

typeof(Program).Assembly.GetName().Name;
icecrime
  • 74,451
  • 13
  • 99
  • 111
Jaster
  • 8,255
  • 3
  • 34
  • 60
  • VS show errors on resolve usings. You can use Assembly.GetEntryAssembly().GetName().Name; – Buzzy May 30 '16 at 07:48
  • 4
    Actually it should be typeof(any).GetTypeInfo().Assembly – Thaina Yu Dec 16 '16 at 15:43
  • 1
    @Thaina. I know this is an old comment, but could you explain **why** you should use GetTypeInfo(). – sgmoore Aug 03 '21 at 11:15
  • 2
    @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 – Thaina Yu Aug 03 '21 at 15:56
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

kiran
  • 1,062
  • 24
  • 31
  • 4
    I got error because of parameter of GetAssemblyName method. I think it should have been `Assembly.GetExecutingAssembly().Location` instead of `Assembly.GetExecutingAssembly().FullName`. – uzay95 Mar 11 '16 at 08:05
10

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;
}
Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
Jim Lahman
  • 2,691
  • 2
  • 25
  • 21
  • 2
    Just be glad you're not calling that from within an Office Addin - where GetEntryAssembly() will return null – PandaWood Aug 14 '18 at 14:23
  • In my case I need executing exe name rather than library dll name. This works very well – Val Aug 25 '20 at 04:33
3

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

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

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"
Rafi Henig
  • 5,950
  • 2
  • 16
  • 36
1

Assembly.GetExecutingAssembly().Location

ivan.ukr
  • 2,853
  • 1
  • 23
  • 41