Programming
Code Assembly
Software Development
Application Path
.NET Framework

How do I get the path of the assembly the code is in?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In .NET, the right API depends on which assembly path you actually want. The assembly that contains the currently executing code, the entry assembly, the calling assembly, and the application's base directory are related ideas, but they are not interchangeable.

That is why there is no single always-correct answer. Assembly.GetExecutingAssembly().Location is common, but sometimes typeof(MyType).Assembly.Location or AppContext.BaseDirectory is the better tool.

Get the Assembly Containing the Current Code

If you want the path of the assembly where the current method is compiled, Assembly.GetExecutingAssembly() is the usual starting point.

csharp
1using System.Reflection;
2
3string path = Assembly.GetExecutingAssembly().Location;
4Console.WriteLine(path);

This returns the file path for the assembly that contains the code currently executing.

That is often good enough for library-internal resource lookup or diagnostics.

Use typeof(SomeType).Assembly When You Mean a Specific Assembly

Sometimes the clearest answer is not "executing assembly" but "the assembly that contains this known type."

csharp
1using System.Reflection;
2
3string path = typeof(MyService).Assembly.Location;
4Console.WriteLine(path);

This is often better inside shared libraries because it makes the target explicit and avoids ambiguity about which code path is currently executing.

Entry Assembly vs. Executing Assembly

Assembly.GetEntryAssembly() is different.

csharp
Assembly? entry = Assembly.GetEntryAssembly();
Console.WriteLine(entry?.Location);

The entry assembly is the process entry point, usually the main executable. If your code is inside a class library loaded by that executable, the entry assembly path is not the same as the library path.

This distinction matters in plugin systems, test runners, ASP.NET hosts, and other environments where your code is not the process entry point.

Base Directory Is Sometimes What You Really Want

If the real goal is "where should I load files relative to the running app," AppContext.BaseDirectory is often a better fit than an assembly path.

csharp
string baseDir = AppContext.BaseDirectory;
Console.WriteLine(baseDir);

This is often more appropriate for:

  • config files next to the app
  • content files deployed with the app
  • resolving relative file paths at runtime

It answers a different question from assembly location, but it is often the more useful one in practice.

Be Careful with CodeBase

Older examples sometimes use Assembly.CodeBase. That returns a URI-style location rather than a straightforward local file path, and it is generally less convenient for modern code.

If you need a usable filesystem path, Location is usually the better choice.

Test and Hosting Scenarios

Different hosting models can change what these APIs mean in practice.

For example:

  • in unit tests, the entry assembly may be the test runner
  • in ASP.NET, the base directory may matter more than a library path
  • in plugin systems, typeof(MyPlugin).Assembly.Location is often the safest explicit choice

So before choosing an API, ask which of these you actually want:

  • the library containing this type
  • the executable that launched the process
  • the directory the app runs from

Common Pitfalls

The biggest mistake is using GetEntryAssembly() when you actually want the library that contains the current code. In many hosted environments, those are not the same thing.

Another common issue is using an assembly file path when what you really need is the application's base directory.

People also copy old CodeBase examples without realizing they are dealing with URI-style values rather than ordinary local paths.

Finally, test your chosen API in the real host environment, not just in a small console app, because assembly-path semantics are context-sensitive.

Summary

  • 'Assembly.GetExecutingAssembly().Location gets the currently executing assembly path.'
  • 'typeof(MyType).Assembly.Location is often the clearest explicit assembly lookup.'
  • 'Assembly.GetEntryAssembly() refers to the process entry assembly, not necessarily your library.'
  • 'AppContext.BaseDirectory is often better when you need the app's runtime directory.'
  • 'CodeBase is usually less convenient than Location for modern code.'
  • Pick the API based on whether you want a library path, entry executable path, or runtime base directory.

Course illustration
Course illustration

All Rights Reserved.