0

I am using Visual Studio 2019 and am trying to get the name of the path of a file that is in the same folder in order to unit test.

So far I have used this code: var directory = System.AppDomain.CurrentDomain.BaseDirectory which I hoped would get the base directory. However it returns the correct path WITH an extra: "\bin\Debug\netcoreap2.2" at the end, which I want to remove from the path.

I have tried cleaning and rebuilding my project. I have tried deleting the files from the bin folder. However this does not work.

Does anyone know the solution for this please?

Jordan1993
  • 864
  • 1
  • 10
  • 28
  • 2
    Does this answer your question? [How do you get the current project directory from C# code when creating a custom MSBuild task?](https://stackoverflow.com/questions/816566/how-do-you-get-the-current-project-directory-from-c-sharp-code-when-creating-a-c) – dns_nx Jan 27 '20 at 11:04
  • 1
    Yes, thank you - I hadn't come across this one in my search @dns_nx This solved my problem: `Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName;` – Jordan1993 Jan 27 '20 at 11:09
  • https://stackoverflow.com/a/15653938/15060659 – feyyum Jan 12 '23 at 18:33

1 Answers1

0

Use a relative path and set "Copy to output" to "always" or "when newer" in the file properties. There's no reason your test code should go about searching for files in the other manner.

Tanveer Badar
  • 5,438
  • 2
  • 27
  • 32