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.
For more details on Embedded Resource, please refer Embed and read files in Windows Application (Windows Forms) in C# and VB.Net.
[
    {
        "CustomerId": 1,
        "Name": "John Hammond",
        "Country": "United States"
    },
    {
        "CustomerId": 2,
        "Name": "Mudassar Khan",
        "Country": "India"
    },
    {
        "CustomerId": 3,
        "Name": "Suzanne Mathews",
        "Country": "France"
    },
    {
        "CustomerId": 4,
        "Name": "Robert Schidner",
        "Country": "Russia"
    }
]
 
 
Form Design
The following Form consists of one Button and one TextBox control.
Read JSON file from Embedded Resources in C# and VB.Net
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.IO;
using System.Reflection;
 
VB.Net
Imports System.IO
Imports System.Reflection
 
 
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.
C#
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();
}
 
VB.Net
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()
End Sub
 
 
Screenshot
Read JSON file from Embedded Resources in C# and VB.Net
 
 
Downloads