222

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
  • 97,705
  • 120
  • 409
  • 613
Patrick
  • 7,783
  • 7
  • 52
  • 71

5 Answers5

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

or

typeof(Program).Assembly.GetName().Name;
icecrime
  • 71,079
  • 13
  • 98
  • 110
Jaster
  • 7,821
  • 2
  • 32
  • 55
  • VS show errors on resolve usings. You can use Assembly.GetEntryAssembly().GetName().Name; – Butsaty May 30 '16 at 07:48
  • 4
    Actually it should be typeof(any).GetTypeInfo().Assembly – Thaina Yu Dec 16 '16 at 15:43
  • @Thaina. I know this is an old comment, but could you explain **why** you should use GetTypeInfo(). – sgmoore Aug 03 '21 at 11:15
  • 1
    @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
9

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
  • 97,705
  • 120
  • 409
  • 613
Jim Lahman
  • 2,643
  • 2
  • 24
  • 20
  • 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
4

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
  • 941
  • 23
  • 28
  • 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
3

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

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

1

Assembly.GetExecutingAssembly().Location

ivan.ukr
  • 2,510
  • 19
  • 38