2

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: enter image description here

  • 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.

Ken White
  • 123,280
  • 14
  • 225
  • 444
Ryan Kanno
  • 105
  • 8
  • Maybe this helps? https://yetanotherchris.dev/csharp/6-ways-to-get-the-current-directory-in-csharp/ – jason.kaisersmith Sep 28 '21 at 05:29
  • Have you tried Assembly.GetExecutingAssembly().Location to get the directory name – The Lemon Sep 28 '21 at 05:40
  • Can you show file structure of `File.json` – Prasad Telkikar Sep 28 '21 at 05:44
  • 3
    "I have a .json file in the same directory as my unit test." - the problem is your expectation that that is the current directory. As per the exception, the current working directory of the process is actually `C:\Path\Tests\bin\Debug\net5.0`. I would suggest embedding the resource in your test assembly and accessing it that way, rather than as a file. – Jon Skeet Sep 28 '21 at 05:45
  • I've also tried using Assembly.GetExecutingAssebly() I get the same result. – Ryan Kanno Sep 28 '21 at 18:44
  • If you copy ‘File.json’ to another directory, can visual studio visit it? – Xingyu Zhao Sep 29 '21 at 03:22
  • check this out https://stackoverflow.com/questions/13762338/read-files-from-a-folder-present-in-project – Manoj Sep 29 '21 at 06:29
  • string path = Path.Combine(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()), @"ServerTests\Models\JsonObjectAsArray.json"); string[] files = System.IO.File.ReadAllLines(path); – Manoj Sep 29 '21 at 06:45
  • @Manoj I've tried that and still get the same error. – Ryan Kanno Sep 30 '21 at 18:14
  • @JonSkeet How do I embed the resource in the test assembly? I assume you mean to put it in the \bin\Debug\net5.0 directory correct? I tried looking it up before asking this question, but haven't found a good answer. Thank you for your input. – Ryan Kanno Sep 30 '21 at 18:16
  • No, I don't - I mean make it an embedded resource *inside* the DLL. If you bring up the properties for that item in Visual Studio, there'll be an option for "Build action" - set that to "Embedded resource". Then you can use `Assembly.GetManifestResourceStream()` to read the resource - see https://github.com/googleapis/google-cloud-dotnet/blob/13ed74b0e70f5b9932c9f679a3378493586c2fcc/apis/Google.Cloud.BigQuery.V2/Google.Cloud.BigQuery.V2.IntegrationTests/UploadTest.cs#L214 for an example. – Jon Skeet Sep 30 '21 at 19:06
  • @JonSkeet Thank you for trying to help me. I looked at your provided link and edited my question above. GetManifestResourceStream() keeps returning null and I can't figure out why. I made sure Build Action is set to Embedded resource, and Copy to Output Directory is set to Copy always. I read on another post that someone changed the Build Action to Resource instead of Embedded resource and that worked for them, but for me it still returns null. I continuing to look up why GetManifestResourceStream() keeps returning null. – Ryan Kanno Sep 30 '21 at 22:52
  • In that last edit you use `JsonObjectAsArray.txt`, but higher up it's either `JsonObjectAsArray.json` or `File.json`. Maybe you should double check that you got it right. – 500 - Internal Server Error Sep 30 '21 at 23:18
  • I would suggest using `Assembly.GetManifestResourceNames` so that you can see which resources are available. – Jon Skeet Oct 01 '21 at 05:41
  • @500-InternalServerError In the edited section of my post I mention that I first tried JsonObjectAsArray.json. Then I tried again by removing the .json file and re adding it as a .txt file, and tried JsonObjectAsArray.txt. File.json was just an example name. – Ryan Kanno Oct 01 '21 at 21:49
  • @JonSkeet I tried using Assembly.GetManifestResourceNames(). When I step through the code I can see the variable shows the correct name. However GetManifestResourceStream() still returns null. – Ryan Kanno Oct 01 '21 at 21:51
  • I would suggest asking a new question with a [mcve] then. I *suspect* there's a typo somewhere in the name you're passing into GetManifestResourceStream, but we can't tell without more details, and they're not really suitable for comments. – Jon Skeet Oct 02 '21 at 06:02

0 Answers0