In this article I will explain with an example, how to embed and read files in Windows Application (Windows Forms) in C# and VB.Net.
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 an embedded Text file and Image file in Windows Application (Windows Forms) in C# and VB.Net.
 
 
Embedded files and their Advantages
Embedded files are called as Embedded Resources and these files can be accessed at runtime using the Assembly class of the System.Reflection namespace.
Any file within the project can be made into an embedded file.
 
Advantages
1. The advantage of embedding files into the Manifest is that one can easily compile the file with the program and thus there is no chances of accidental delete or misplace.
2. Since the file is embedded, there is no need of using any Folder Paths or Locations to access the file.
 
Limitation
1. The only limitation is that once you embed a file, it cannot be updated again unless you update the file and recompile the program.
 
 
Adding an Embedded File
1. The very first thing is to create a Folder to the Project and then add the file to be embedded within that folder by making using of Add then Existing Item option from the Context Menu of the Solution Explorer as shown below.
Embed and read files in Windows Application (Windows Forms) in C# and VB.Net
 
2. Once the files to be embedded are added, then you will need to right click the File and select Properties option from the Context Menu as shown below.
Embed and read files in Windows Application (Windows Forms) in C# and VB.Net
 
3. Finally you will need to change the Build Action property value from Content to Embedded Resource as shown below.
Embed and read files in Windows Application (Windows Forms) in C# and VB.Net
 
 
Form Controls
The following Form consists of two Buttons, one TextBox control and one PictureBox control.
Embed and read files in Windows Application (Windows Forms) 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 Embedded Text file
When the Button is clicked, first an object of the Assembly class is created and it is assigned the reference of the executing assembly.
Then the contents of the Text file are read using a 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_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_File_VB.Sample.txt"))
    txtTextFile.Text = reader.ReadToEnd()
End Sub
 
 
Reading Embedded Image file
When the Button is clicked, first an object of the Assembly class is created and it is assigned the reference of the executing assembly.
Then the contents of the Image file are read using a Stream 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 Stream class object is converted into a Bitmap class object which is ultimately assigned to the Image property of the PictureBox control.
C#
private void btnImage_Click(object sender, EventArgs e)
{
    Assembly assembly = Assembly.GetExecutingAssembly();
    Stream stream = assembly.GetManifestResourceStream("Embed_File_CS.Files.Mudassar.jpg");
    picImageFile.Image = new Bitmap(stream);
}
 
VB.Net
Private Sub btnImage_Click(sender As System.Object, e As System.EventArgs) Handles btnImage.Click
    Dim assmbly As Assembly = Assembly.GetExecutingAssembly()
    Dim stream As Stream = assmbly.GetManifestResourceStream("Embed_File_VB.Mudassar.jpg")
    picImageFile.Image = New Bitmap(stream)
End Sub
 
 
Screenshots
Reading Text file
Embed and read files in Windows Application (Windows Forms) in C# and VB.Net
 
Reading Image file
Embed and read files in Windows Application (Windows Forms) in C# and VB.Net
 
 
Downloads