I'm trying to write a unit test for a JsonConverter. I have a .json file in the same directory as my unit test. I've tried using these methods to read the file:
- string directory = File.ReadAllText("File.json");
- string directory = File.ReadAllText(@"....\File.json");
- string directory = Path.Combine(Directory.GetCurrentDirectory(), @"\File.json");
- string directory = File.ReadAllLines(@"\File.json");
- string directory - File.ReadAllLines(@"....\File.json");
- string directory = File.OpenText("File.json");
- I've also opened the properties of my File.json and made sure my Copy To Output Directory is set to Copy Always
- I've also made sure I'm running Visual Studio as administrator.
- I've deleted the File.json and added it again (Thought maybe there could be an under-the-hood issue where VS didn't know it was there)
I keep getting this error: System.IO.FileNotFoundException: 'Could not find file C:\Path\Tests\bin\Debug\net5.0\File.json'
None of these have worked for me so far. I don't know what I'm doing wrong, or if there's a setting in Visual Studio that I don't know about. Any advice would be greatly appreciated. Thank you.
[Edit]
Here is a picture of my file structure:
- I've since tried opening the properties to make sure Copy to Output Directory is set to Copy always.
- I've also tried setting the file's property's Build Action to Embedded resource.
- I've also tried opening ServerTests > Properties > Build > Advanced > Debugging information > Full
When I copy paste the full path I get: C:\Path\ServerTests\Models\JsonObjectAsArray.json When I run the test, I still get the error: 'Could not find file C:\Path\ServerTests\Models\bin\Debug\net5.0\JsonObjectAsArray.json
I'm continuing to look for other settings related solutions but have yet to find any that work.
[Edit] I have since tried:
- string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssebmly().Location), @"JsonObjectAsArray.json");
- string[] file = File.ReadAllLines(path);
I still get the same error: 'Could not find file 'C:\Path\ServerTests\bin\Debug\net5.0\JsonObjectAsArray.json'
I've tried deleting the file and adding it back into the project by right clicking the folder and selecting 'Add > Existing item'
I've tried the suggestion by @Manoj in the comments below, and clicked on the link Read files from a Folder present in project which contains the same solution that I just provided. I still get the same error.
Any other advice would be greatly appreciated. I can't help but think there might be a settings issue that I'm unaware of. For now I just included the raw text from my json file as a string directly in my test function, but ideally I'd prefer to have it as a separate file to keep the test file cleaner.
[Edit] I have since tried:
TypeInfo typeInfo = typeof(JsonConverterTests).GetTypeInfo();
string resourceName = typeInfo.Namespace + "JsonObjectAsArray.txt";
Stream stream = typeInfo.Assembly.GetManifestResourceStream(resourceName);
StreamReader reader = new(stream);
string json = reader.ReadToEnd();
For this snippet the code fails at GetManifestResourceStream(resourceName); The Stream always returns null. I've made sure the properties of my resource file is set to Build Action > Embedded resource, and Copy to Output Directory > Copy always.
I've tried removing my original .json file and replacing it with a plain .txt file instead, which returns null for the Stream. I've also tried my above methods again to try to read the .txt file, but I still get the same error as above.
I've been researching why GetManifestResourceStream returns null even if the file is in the same folder, but most of the solutions are similar to the code provided above. Will continue to look up why GetManifestResourceStream keeps returning null. Any help would be greatly appreciated. Thank you.