In this article I will explain with an example, how to use Manifest File in Windows Forms (WinForms) Application using 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

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. Create a Folder inside the Project and then add the file to be embedded within that folder by Right Click on Solution Explorer and select Add, then select Existing Item option from the Context Menu as shown below.
Using Manifest File in Windows Application with C# and VB.Net
 
2. Once the files to be embedded are added then, Right Click on the File and select Properties option from the Context Menu as shown below.
Using Manifest File in Windows Application with C# and VB.Net
 
3. Finally, change the Build Action property from Content to Embedded Resource as shown below.
Using Manifest File in Windows Application with C# and VB.Net
 
 

Form Design

The following Form consists of two Buttons, one TextBox control and one PictureBox control.
Using Manifest File in Windows Application with 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 Read Text File Button is clicked, following event handler is executed.
Inside this event handler, an object of the Assembly class is created and assigned with 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_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 Read Image File Button is clicked, following event handler is executed.
Inside this event handler, an object of the Assembly class is created and assigned with the reference of the executing assembly using GetExecutingAssembly method.
Then, the contents of the Image file are read using a Stream class object using the GetManifestResourceStream function of the Assembly class.
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
 
 

Screenshot

Using Manifest File in Windows Application with C# and VB.Net
 
 

Downloads