Skip to content

C#

Visual Studio - Windows Forms Embed Resources

Creating a folder and Embedding a resource

Create a Folder, this is the Folder that will contain our Embedded File

alt text

I named the Folder Files

alt text

You can now add a file to that Folder

I added file example_file.txt

alt text

Right click the file > Properties

alt text

Change the BuildAction to Embedded resource

alt text

Creating the Visual Form

I added a Button and a multiline TextBox. I also changed the names on both items so we know what I am calling.

alt text

alt text

alt text

The Code part

The namespace

On the Visual Form, double click the Button

alt text

At the of the page, add the following

using System.IO;
using System.Reflection;

In the Button function, add the following code

Assembly asm = Assembly.GetExecutingAssembly();
string[] names = asm.GetManifestResourceNames();
foreach (var name in names) { textBox_Embedded_File_Contents.Text = name + Environment.NewLine; }

What we will use above code for is only to get the namespace call for the text file so we can use it in the next steps.

So far this is what the code looks like

alt text

First run F5, Click the Button. Now we can see how the file is being called with the namespace. So it's doing ..

alt text

Reading the file content

Now that we know the namespace call for this file, we will remove that code section and replace it with the following

Assembly asm = Assembly.GetExecutingAssembly();
StreamReader reader = new StreamReader(asm.GetManifestResourceStream("Example.Files.example_file.txt")); //ConfigurationRepeater is the namespace of the project
textBox_Embedded_File_Contents.Text = reader.ReadToEnd();

This is what the code looks like now

alt text

Run the form again F5, Click the Button and it should read the Embedded File contents and print them to the TextBox.

alt text

It indeed printed the file contents correctly

alt text