bsky
bsky

Reputation: 20242

Get the root folder of the current project

I want to get the folder in which my current project is located.

The only way I know to access something in my current folder is this:

System.Reflection.Assembly.GetExecutingAssembly().Location

However, that takes me to an address of the form:

Z:\path\to\my\things\MyProject\bin\x86\Debug\MyProject.dll

Instead of getting to this address, I would just want to get to:

Z:\path\to\my\things\MyProject

I realised that I can achieve that by doing this:

Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "../../..")

However, I don't like that I have to hardcode "../../..".

Is there a way of achieving the same thing without the hardcoding?

EDIT

If I try Environment.CurrentDirectory or System.IO.Directory.GetCurrentDirectory();, as suggested in this question, I get to C:\Windows\System32, so that's not helpful at all.

Upvotes: 1

Views: 5539

Answers (1)

Zhaph - Ben Duguid
Zhaph - Ben Duguid

Reputation: 26976

Based on your comment I would leave the code as-is - you're right that hardcoding the ..\..\..\ isn't the way to go - and getting the path to the executing assembly is the correct way to do this.

Instead what you should for you unit test project is include the test file in your test project, and set the properties of the file to be "Content" and "Copy to output directory"

That will move the test file into the same location as the DLL, and then you'll be able to reference it correctly.

Upvotes: 2

Related Questions