46

What is the best way to get the application name (i.e MyApplication.exe) of the executing assembly from a referenced class library in C#?

I need to open the application's app.config to retrieve some appSettings variables for the referenced DLL.

mdb
  • 52,000
  • 11
  • 64
  • 62
Michael Kniskern
  • 24,792
  • 68
  • 164
  • 231

8 Answers8

63

To get the answer to the question title:

// Full-name, e.g. MyApplication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
string exeAssembly = Assembly.GetEntryAssembly().FullName;

// or just the "assembly name" part (e.g. "MyApplication")
string exeAssemblyName = Assembly.GetEntryAssembly().GetName().Name;

As mentioned by @Ben, since you mention wanting to get the configuration information, use the ConfigurationManager class.

Ray Hayes
  • 14,896
  • 8
  • 53
  • 78
  • 1
    That requires using System.Reflection? – Michael Kniskern Nov 07 '08 at 17:26
  • 1
    Yes, that's in the System.Reflection namespace. – Judah Gabriel Himango Nov 07 '08 at 17:29
  • This was returned from that call: MyApplication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null – Michael Kniskern Nov 07 '08 at 17:32
  • To find out which is the required namespace just select in the above code the Assembly and Ctrl + Shift + F10 ... – Yordan Georgiev May 28 '09 at 05:35
  • 2
    That is so nice of you, that about two years pass and you edit your answer to include my solution. What took you so long? :P – Aoi Karasu Mar 29 '11 at 06:15
  • 1
    `Assembly.GetEntryAssembly()` returns null for an ASP.net web-site. `Assembly.GetExecutingAssembly()` returns something. – Ian Boyd Aug 20 '13 at 13:40
  • AppDomain can be a EXE application, Web application, Unit test application, Addin Visual Studio, and "Silverlight App"(?). Maybe interesting full solution for all cases. – Kiquenet Mar 26 '14 at 08:01
  • I have Service.Host.dll and SvcImpl.dll (<%@ ServiceHost Service="SvcImpl" %>) Service Host in IIS. This code in SvcImpl.dll: 1) Assembly.GetEntryAssembly() gets null. 2) Assembly.GetExecutingAssembly() gets SvcImpl. 3) Assembly.GetCallingAssembly(); gets System.ServiceModel. How can I get Service.Host programatically in SvcImpl if Assembly.GetEntryAssembly() gets null? – Kiquenet Mar 27 '14 at 14:32
18

If you want to get the current appdomain's config file, then all you need to do is:

ConfigurationManager.AppSettings....

(this requires a reference to System.Configuration of course).

To answer your question, you can do it as Ray said (or use Assembly.GetExecutingAssembly().FullName) but I think the problem is easier solved using ConfigurationManager.

Ben Scheirman
  • 40,531
  • 21
  • 102
  • 137
  • Will this solution work with asp.net application? The referenced class library can be used by WinForms and ASP.NET apps – Michael Kniskern Nov 07 '08 at 17:29
  • 1
    yep, that's the beauty of ConfigurationManager. It selects whichever app.config/web.config is appropriate for the app domain. This was introduced in .NET 2.0. – Ben Scheirman Nov 07 '08 at 17:50
  • I just ran a test with an asp.net application and a WinFomrs application and it worked for both environments. I am currently running a test with a WCF service. This is web I was running into issues. – Michael Kniskern Nov 07 '08 at 17:58
  • Is the dll you're talking about in a separate app domain? – Ben Scheirman Nov 07 '08 at 18:52
  • 1
    This worked for ASP.NET and WinForms application, but it does not work for a WCF service. – Michael Kniskern Nov 07 '08 at 18:52
  • @Ben - How would I figure that out? – Michael Kniskern Nov 07 '08 at 18:55
  • 18
    `Assembly.GetExecutingAssembly()` does not get the assembly the OP is looking for. `Assembly.GetEntryAssembly()` does – quentin-starin Apr 25 '11 at 15:34
  • I have Service.Host.dll and SvcImpl.dll ( <%@ ServiceHost Service="SvcImpl" %>). Service Host in IIS. This code in SvcImpl.dll: 1) Assembly.GetEntryAssembly() gets null. 2) Assembly.GetExecutingAssembly() gets SvcImpl. 3) Assembly.GetCallingAssembly(); gets System.ServiceModel. How can I get Service.Host programatically in SvcImpl if Assembly.GetEntryAssembly() gets null? – Kiquenet Mar 27 '14 at 14:30
  • Assembly.GetExecutingAssembly().FullName doesn't give Application.exe name – RJN May 15 '20 at 08:02
11

To get the exact name without versions, etc. use:

string appName = Assembly.GetEntryAssembly().GetName().Name;

Works with .NET v1.1 and later.

Aoi Karasu
  • 3,730
  • 3
  • 37
  • 61
  • If A.exe uses B.dll, and B.dll uses C.dll, what's happens if that code is in C.dll ? – Kiquenet Mar 26 '14 at 07:58
  • 2
    Still returns A.exe, unless A.exe is unmanaged, or any of the dlls is loaded into separate AppDomain. For details see: http://msdn.microsoft.com/en-us/library/system.reflection.assembly.getentryassembly(v=vs.110).aspx – Aoi Karasu Mar 26 '14 at 13:09
  • I have Service.Host.dll and SvcImpl.dll (<%@ ServiceHost Service="SvcImpl" %>) Service Host in IIS. This code in SvcImpl.dll: 1) Assembly.GetEntryAssembly() gets null. 2) Assembly.GetExecutingAssembly() gets SvcImpl. 3) Assembly.GetCallingAssembly(); gets System.ServiceModel. How can I get Service.Host programatically in SvcImpl if Assembly.GetEntryAssembly() gets null? – Kiquenet Mar 27 '14 at 14:40
9

You can get the assembly through the class type...

typeof(MyClass).Assembly
Jaider
  • 14,268
  • 5
  • 75
  • 82
9

If you want the name of the parent EXE and not the referenced DLL assembly - you will need to use this:

Assembly.GetEntryAssembly().GetName().Name

This will return the EXE name (minus the .EXE part).

Using GetExecutingAssembly() is not right as per the OP's question (first paragraph of it!) as it will return the DLL name.

James Harcourt
  • 6,017
  • 4
  • 22
  • 42
5

You should never couple your libraries to a consumer (in this case Web, WinForm or WCF app). If your library needs configuration settings, then GIVE it to the library.

Don't code the library to pull that data from a consumer's config file. Provide overloaded constructors for this (that's what they are for).

If you've ever looked at the ConfigurationManager.AppSettings object, it is simply a NameValueCollection. So create a constructor in your library to accept a NameValueCollection and have your consumer GIVE that data to the library.

//Library
public class MyComponent
{
  //Constructor
  public MyComponent(NameValueCollection settings)
  {
     //do something with your settings now, like assign to a local collection
  }
}

//Consumer
class Program
{
  static void Main(string[] args)
  {
    MyComponent component = new MyComponent(ConfigurationManager.AppSettings);
  }
}
George Stocker
  • 57,289
  • 29
  • 176
  • 237
2

If you want to read (and display) version number:

  Assembly ass = System.Reflection.Assembly.GetExecutingAssembly();
  AssemblyName assname = ass.GetName();

  Version ver=assname.Version;

Somewhere in application (ie Title block in a Windows form)

 this.Text = "Your title    Version " + ver;
Jon Egerton
  • 40,401
  • 11
  • 97
  • 129
A Reynaldos
  • 131
  • 1
  • 2
1

For the short name of an assembly from a class instance:

Me.GetType ().Assembly.GetName().Name
Laurel
  • 5,965
  • 14
  • 31
  • 57