Make a text file a project resource in C#

[project resource]

This is handy for including a large amount of text as a project resource instead of putting the text directly in the code or 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.

Download Example   Follow me on Twitter   RSS feed   Donate

This entry was posted in files and tagged , , , , , , , , . Bookmark the permalink.