Conflicting File Paths when trying to Read in a JSON file
I have a project that reads in a JSON file that is stored in that project. I am trying to write unit tests for this project, but when the project tries to read the JSON file it looks in the wrong directory.
For the file path I used Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) combined with the JSON file name appended at the end.
However while this should result in project/file.json, when the unit test runs it results in
Test project/bin/Debug/..../file.json.
How can I fix the file path so that it properly accesses the file when other C# projects call the file?
2 Replies
I should probably mention that the JSON files aren't showing up in the build folder. Which is actually probably the problem here
That's because your executing assembly is in the output location
project/bin/Debug
not the project root. You can do two things:
1. Mark the json file to copy to the output directory.
2. Embed the json file in the test binary and read it as an embedded resource