Within a project folder I have a subfolder docs
, which contains a number of documents such as .json files that are used within my application.
I want to be able to access these files during both normal execution and when debugging, especially when I need to debug tests.
At the moment I have the following line that reads in data from a specific file:
string data = System.IO.File.ReadAllText("./Docs/file.json");
This works normally, but when I debug my application I get the following exception
System.IO.DirectoryNotFoundException: 'Could not find a part of the path
'C:\Project\Project.Test\bin\Debug\net
coreapp2.1\Docs\file.json'.'
I assume this is because when the debugger is ran it is executing from a different location to normal, and therefore the relative path used above is different and thus the file can't be found.
To try and rectify this I used Directory.GetCurrentDirectory()
, as below:
data = System.IO.File.ReadAllText(Directory.GetCurrentDirectory() + @"\Docs\file.json");
But this produced exactly the same exception, what do I need to change so that I can use the relative path of my project and find the file I need under every circumstance?
Comments
Post a Comment