In this article I will explain with an example, how to embed and read Text file from Resources in C# and VB.Net.
 
 
Adding an Embedded File
For more details on Embedded Resource, please refer Embed and read files in Windows Application (Windows Forms) in C# and VB.Net.
 
 
Form Design
The following Form consists of one Button and one TextBox control.
Read Text 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 Text file from Embedded Resources
When the Read Text 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 Text 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 btnText_Click(object sender, EventArgs e)
{
    Assembly assembly = Assembly.GetExecutingAssembly();
    StreamReader reader = new StreamReader(assembly.GetManifestResourceStream("Embed_Text_File_CS.Files.Sample.txt"));
    txtTextFile.Text = reader.ReadToEnd();
}
 
VB.Net
Private Sub btnText_Click(sender As System.Object, e As System.EventArgs) Handles btnText.Click
    Dim assmbly As Assembly = Assembly.GetExecutingAssembly()
    Dim reader As New StreamReader(assmbly.GetManifestResourceStream("Embed_Text_File_VB.Sample.txt"))
    txtTextFile.Text = reader.ReadToEnd()
End Sub
 
 
Screenshot
Read Text File from Embedded Resources in C# and VB.Net
 
 
Downloads