In this article I will explain with an example, how to embed and read JSON file from Resources in C# and VB.Net.
Adding an Embedded File
Following is the JSON content saved inside a JSON file as Embedded Resource inside the Project.
"Name": "John Hammond",
"Country": "United States"
"Name": "Mudassar Khan",
"Name": "Suzanne Mathews",
"Name": "Robert Schidner",
The following Form consists of one Button and one TextBox control.
You will need to import the following namespaces.
Reading Json file from Embedded Resources
When the Read Json File Button is clicked, an object of the Assembly class is created and assigned the reference of the executing assembly using GetExecutingAssembly method.
Then, the contents of the JSON file are read using StreamReader class object using the GetManifestResourceStream function of the Assembly class.
Note: In C#, you will need to access file using Namespace.FolderName.FileName, but in VB.Net there is no need to specify the name of the Folder, it can be directly accessed as Namespace.FileName.
Finally, the contents of the StreamReader are read using the ReadToEnd method and are assigned to the TextBox control.
private void btnJson_Click(object sender, EventArgs e)
Assembly assembly = Assembly.GetExecutingAssembly();
StreamReader reader = new StreamReader(assembly.GetManifestResourceStream("Embed_Json_File_CS.Files.Sample.json"));
txtJson.Text = reader.ReadToEnd();
Private Sub btnJson_Click(sender As System.Object, e As System.EventArgs) Handles btnJson.Click
Dim assmbly As Assembly = Assembly.GetExecutingAssembly()
Dim reader As New StreamReader(assmbly.GetManifestResourceStream("Embed_Json_File_VB.Sample.json"))
txtJson.Text = reader.ReadToEnd()