I'm trying to read in a JSON file from an embedded resource. I've tried looking online but most questions are about .txt files that aren't helping.
I've already tried making the json file an embedded resource but that hasn't helped either.
var assembly = Assembly.GetExecutingAssembly();
var resourceName = "MyLibrary.Properties.Resources.MyJson.json";
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
using (StreamReader reader = new StreamReader(stream))
{
string jsonFile = reader.ReadToEnd(); //Make string equal to full file
}
When this code is run, I get the following error: System.ArgumentNullException: 'Value cannot be null. Parameter name: stream'
Method 1: Add existing file, set property to Embedded Resource. Add the file to your project, then set the type to Embedded Resource . NOTE: If you add the file using this method, you can use GetManifestResourceStream to access it (see answer from @dtb).
Embedded files are called as Embedded Resources and these files can be accessed at runtime using the Assembly class of the System.Reflection namespace. This article will illustrate, how to read and display an embedded Text file and Image file in Windows Application (Windows Forms) in C# and VB.Net.
Open Solution Explorer add files you want to embed. Right click on the files then click on Properties . In Properties window and change Build Action to Embedded Resource . After that you should write the embedded resources to file in order to be able to run it.
This was fixed by changing
var resourceName = "MyLibrary.Properties.Resources.MyJson.json";
to this:
var resourceName = "MyLibrary.Resources.MyJson.json";
Having "Properties" in the resourceName is incorrect it seems.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With