25

Please suggest which is the best to getting executing assembly location.

Assembly.GetAssembly(typeof(NUnitTestProject.RGUnitTests)).Location

or

Assembly.GetExecutingAssembly().Location 

Please suggest which can be better. Can I use GetEntryAssembly() also?

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
Neeraj Dubey
  • 4,401
  • 8
  • 30
  • 49

2 Answers2

54

It depends on what you want.

  • Assembly.GetAssembly returns the assembly where type is declared.
  • Assembly.GetExecutingAssembly returns the assembly where the current code is being executed on.
  • Assembly.GetEntryAssembly returns the process executable. Keep in mind that this may not be your executable.

For example, imagine your code is on myexecutable.exe.

trdparty.exe uses Assembly.LoadFile to load your executable and run some code by reflection.

myexecutable.exe uses type MyClass

but the trdparty.exe patches your code to use the new version of MyClass located in Patch.dll.

So now, if you run your application all by itself, you get this result:

Assembly.GetAssembly(typeof(MyClass)) -> myexecutable.exe
Assembly.GetExecutingAssembly() -> myexecutable.exe
Assembly.GetEntryAssembly() -> myexecutable.exe

but if you have the scenario mentioned above, you get:

Assembly.GetAssembly(typeof(MyClass)) -> Patch.dll
Assembly.GetExecutingAssembly() -> myexecutable.exe
Assembly.GetEntryAssembly() -> trdparty.exe

So as a response, you should use the one that provides the result you want. The answer may seem obvious that it is Assembly.GetExecutingAssembly(), but sometimes it's not. Imagine that you are trying to load the application.config file associated with the executable, then the path will most probably be Assembly.GetEntryAssembly().Location to always get the path of the "process".

As I said, it depends on the scenario and the purpose.

Pang
  • 9,564
  • 146
  • 81
  • 122
CaldasGSM
  • 3,032
  • 16
  • 26
  • In my case 'Assembly.GetEntryAssembly()' is null .Rest two are given `dll` path – Neeraj Dubey Nov 21 '14 at 11:19
  • documentation says "can return null when a managed assembly has been loaded from an unmanaged application" – CaldasGSM Nov 21 '14 at 11:22
  • But i am using `C#.net` which is managed application. I dont know if `NUnit` dll is managed or unmanaged. – Neeraj Dubey Nov 21 '14 at 11:25
  • is it a web aplication? – CaldasGSM Nov 21 '14 at 11:27
  • No its a `NUnit` test library project which is created in `C#.net` – Neeraj Dubey Nov 21 '14 at 11:28
  • 1
    if you say the other 2 are returning your dll.. and if it is what you want.. them you should use `Assembly.GetAssembly(typeof(MyClass))` or its equivalent `typeof(Example).Assembly` because MSDN says `GetExecutingAssembly` as worse performance – CaldasGSM Nov 21 '14 at 11:33
  • 1
    GetEntryAssembly() is null in ASP.NET. It is set in ASP.NET Core. – Der_Meister Aug 24 '18 at 07:41
  • @Der_Meister your last comment, this is what i am seeing, shouldn't they be the same regardless, its making it confusing as to which one to use – Seabizkit Mar 23 '20 at 16:39
  • @Seabizkit I did not test, but I assume that the entry assembly will be set in reverse proxy mode. ASP.NET Core also supports IIS in-process hosting model - I think the entry assembly will be null in that case. https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/?view=aspnetcore-3.1#in-process-hosting-model – Der_Meister Mar 23 '20 at 18:50
  • For .NET I have a method that works for both desktop and web apps: https://github.com/Saritasa/dotnet-deploy/blob/asp-net/src/DeployDemo.Web/Support/Util.cs – Der_Meister Mar 23 '20 at 18:53
  • @Der_Meister The GetEntryAssembly() is null in ASP.NET. because the process executable is not a .net assembly, it is a IIS process, probably w3wp.exe which is a native windows assembly.. in that case one of the other methods should be called.. – CaldasGSM Mar 23 '20 at 20:10
  • @CaldasGSM that makes a little more sense... would be neat to have a proper write up about it. – Seabizkit Mar 23 '20 at 20:11
-3

It seems rather obvious: When you want the executing assembly, use GetExecutingAssembly.

Sometimes you don't have one, for example when running as an Office add-in. You could use Assembly.GetAssembly instead.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325