Add embedded text file to project and read from them

As you might have seen from my previous SCCM Query Creator project, I wanted to have a simple way to have a list from a text file, which I then outputted in a list view, and then, when I select the name, I would read from an internal embedded text file from the project.

Its actually quite simple to achieve this, so I am going to show the simple steps you need to do in order to achieve this in a sample project.

Once you have a project created and opened, in the Solution Explorer on the right, double-click on Properties.

In the new page which opens up, navigate to Resources on the left.

Then, click on Add Resource > Add Existing File.

This will open a file browser dialog. With this, select the desired text file you want included in the project. Once the file is added in the project, select the file from the Solution Explorer on the right. On the bottom, you should see the Properties, if not, right-click the file and select Properties.

Make sure to define the Build Action to Embedded Resource. This will ensure that the txt file is embedded inside the EXE at the time of the build and will allow us to easily access it via some simple code.

Do this for as many files as you want in your project.

In my case, the sample form works like this:

  • The data.txt will contains multiple entries on multiple lines. Each line will represent an option in the ListBox I added in the Form. Each line from the text file will be converted to a list in C#
  • The other files are named exactly like each line in the data.txt file for easier handle

For example, the data.txt contains these two lines:

And the two txt files added in the project are:

Now that the text files are added, it’s time to add the code. To read the contents of the text file embedded in our project (data.txt), it’s as simple as:

string salut = Properties.Resources.data;

 

After Properties.Resources, add a dot (.) and the file name (in my case data).

Now, it’s time to initialize a list which will contain strings:

List<string> salutare = new List<string>();

 

As a final step, we need to split the “salut” string which contains the data from our file. The split will be done with “\r\n” which represents a new line:

string[] lines = salut.Split(new string[] { “\r\n” }, StringSplitOptions.None);

 

Each line we find in our string will be added to the list:

foreach (string line in lines)
{
salutare.Add(line);
}

 

And to populate the ListBox, its as easy as:

listBox1.DataSource = salutare;

 

I have placed the code to run when the form is loaded:

private void Form1_Load(object sender, EventArgs e)
{
string salut = Properties.Resources.data;

List<string> salutare = new List<string>();

string[] lines = salut.Split(new string[] { “\r\n” }, StringSplitOptions.None);

foreach (string line in lines)
{
salutare.Add(line);
}

listBox1.DataSource = salutare;

}

 

After we added our code, the listbox should be populated with the two options from the file:

Now its time to read from each internal text file from the project and output the contents in the TextBox. We will need to use the GetManifestResourceStream, so we need to make another function:

public static string ReadTextResourceFromAssembly(string name)
{
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(name))
{
return new StreamReader(stream).ReadToEnd();
}
}

 

When an internal file is passed to the function, this will output the contents.

Next, when the index of the listbox is changed (when we click on different items) we need to output stuff in the Textbox, so the following has been added:

 

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string text = listBox1.GetItemText(listBox1.SelectedItem);
string text2 = “testTextFile.Resources.” + text + “.txt”;
string rezultat = ReadTextResourceFromAssembly(text2);
textBox1.Text = rezultat;

}

 

So what does it do?

string text = listBox1.GetItemText(listBox1.SelectedItem);

With this, we get the name of the selected item in the ListBox

 

string text2 = “testTextFile.Resources.” + text + “.txt”;

With this, we are composing the full path to our internal text file. Pay close attention on how this is built:

ProjectName.Resources + The name of the file which we get from the listbox + .TXT

 

For example,if your project is called TestProject, the string should be:

TestProject.Resources.NAMEOFFILE.txt

 

If you want to get a list of all resources (with their internal path) do the following:

System.Reflection.Assembly thisExe;
thisExe = System.Reflection.Assembly.GetExecutingAssembly();
string[] resources = thisExe.GetManifestResourceNames();
string list = “”;

foreach (string resource in resources)
list += resource + “\r\n”;

textBox1.Text = list;

 

And that is it, the files should be opened accordingly now:

If you want to download the sample project, you can download it from here:

Sample embedded text file CSharp Project

Sharing is caring!

Leave a comment

Your email address will not be published. Required fields are marked *

4 × 5 =