Is there a way to get the path for the assembly in which the current code resides? I do not want the path of the calling assembly, just the one containing the code.
Basically my unit test needs to read some xml test files which are located relative to the dll. I want the path to always resolve correctly regardless of whether the testing dll is run from TestDriven.NET, the MbUnit GUI or something else.
Edit: People seem to be misunderstanding what I'm asking.
My test library is located in say
C:\projects\myapplication\daotests\bin\Debug\daotests.dll
and I would like to get this path:
C:\projects\myapplication\daotests\bin\Debug\
The three suggestions so far fail me when I run from the MbUnit Gui:
-
Environment.CurrentDirectory
gives c:\Program Files\MbUnit -
System.Reflection.Assembly.GetAssembly(typeof(DaoTests)).Location
gives C:\Documents and
Settings\george\Local
Settings\Temp\ ….\DaoTests.dll -
System.Reflection.Assembly.GetExecutingAssembly().Location
gives the same as the previous.
Best Solution
I've defined the following property as we use this often in unit testing.
The
Assembly.Locationproperty sometimes gives you some funny results when using NUnit (where assemblies run from a temporary folder), so I prefer to useCodeBasewhich gives you the path in URI format, thenUriBuild.UnescapeDataStringremoves theFile://at the beginning, andGetDirectoryNamechanges it to the normal windows format.