Embed a text file in a project as a resource in C#

This is handy for including a large amount of text in a project without adding it within the code and without including a file in the program's installation package.

Open the Project menu and select Properties. Open the Add Resource dropdown and select Add Existing File. Select the text file and click Open.

Now the code can use it at run time as shown in the following code.

// Load the file resource.
private void Form1_Load(object sender, EventArgs e)
{
    txtQuotes.Text = Properties.Resources.Twain;
    txtQuotes.Select(0, 0);
}

In this example the text file is called Twain.txt so the program can get its text from Properties.Resources.Twain.

That's all there is to it. A simple trick but sometimes useful.

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.