Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Get an Executable Assembly Name

0.00/5 (No votes)
17 Oct 2008 1  
Use GetEntryAssembly to obtain an executable's assembly name - even from a DLL.

Introduction

This little example shows how you can obtain the assembly name of an executable, even if you seek it from a DLL called by that executable.

Background

I was looking for a reliable way to get the assembly name of the original starting executable. I found plenty of examples of using System.Environment.GetCommandLineArgs()[0], but it returns the path to the executable file name, not the assembly name defined in the Project Properties.

Additionally, I did not want the full path, just the name of the assembly, even if the file name had changed. Sure, I could use System.IO.Path.GetFileNameWithoutExtension(path) to get the name, but that would not allow me to express my true OCD Personality Type nature :). If called within the IDE, you would get "ExeName.vshost".

Finally, I wanted to obtain the name of the original executable assembly, even if from a DLL called by the executable. How did I finally satisfy my own personal obsessive nature and find a hackless solution?

Using the code

The GetEntryAssembly() method returns the assembly of the first executable that was executed. I used the following to obtain the assembly name of the original executable:

C#:

string name = System.Reflection.Assembly.GetEntryAssembly().GetName().Name;

Visual Basic .NET

Dim name as String = System.Reflection.Assembly.GetEntryAssembly().GetName().Name; 

We could use System.Windows.Forms.Application.ProductName in Visual Basic, but it does not return the assembly name - it returns the assembly Product Name (a slight distinction - again that whole OCD thing :)

If you use this code in a DLL file, it will still return the assembly name of the executable that called on the DLL. I use this method in a generic logging class that I call from many different projects - I would like the entries it makes to contain the assembly name of the program that used the DLL. It even returns the original assembly name when the DLL is called from another DLL that was called by the original EXE! (Whew! Time for medication. :)

The sample application produces these message boxes:

AppExample.PNG

from this executable's assembly:

AssemblyName.png

Enjoy!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here