Avatar ·

How to get the path to the assembly from which the current code is executing?

Hello everyone! I'm a beginner C# developer, writing a small project for test automation using MBUnit. I have a problem: I need to get the path to the assembly file from which the current code is executing. Why? I want tests to load test data from a folder located next to the test executable file. If I use Environment.CurrentDirectory, the path may differ in different environments (Visual Studio, console launch, CI server), which leads to "file not found" errors.

Here is an example of the code I tried:

string path = Environment.CurrentDirectory;
Console.WriteLine($"Current directory: {path}");

The problem is that when running tests from MBUnit, this path points to the Visual S