How to get Directory while running unit test

Hi when running my unit test I'm wanting to get the directory my project is running in to retrieve a file.

Say I have a Test project named MyProject. Test I run:

AppDomain.CurrentDomain.SetupInformation.ApplicationBase

and I receive "C:\\Source\\MyProject.Test\\bin\\Debug".

This is close to what I'm after. I don't want the bin\\Debug part.

Anyone know how instead I could get "C:\\Source\\MyProject.Test\\"?


Solution 1:

I would do it differently.

I suggest making that file part of the solution/project. Then right-click -> Properties -> Copy To Output = Copy Always.

That file will then be copied to whatever your output directory is (e.g. C:\Source\MyProject.Test\bin\Debug).

Edit: Copy To Output = Copy if Newer is the better option

Solution 2:

Usually you retrieve your solution directory (or project directory, depending on your solution structure) like this:

string solution_dir = Path.GetDirectoryName( Path.GetDirectoryName(
    TestContext.CurrentContext.TestDirectory ) );

This will give you the parent directory of the "TestResults" folder created by testing projects.

Solution 3:

Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName;

This will give you the directory you need....

as

AppDomain.CurrentDomain.SetupInformation.ApplicationBase 

gives nothing but

Directory.GetCurrentDirectory().

Have alook at this link

http://msdn.microsoft.com/en-us/library/system.appdomain.currentdomain.aspx