Click here to Skip to main content
11,789,710 members (57,330 online)
Click here to Skip to main content

Create String Variables from Embedded Resources Files

, 15 Jun 2004 75.1K 1.3K 27
Rate this:
Please Sign up or sign in to vote.
Use embedded text files as the source for populating large string variables.


As I have written before in a previous article, creating large string variables can be a dull but necessary part of coding. Whether it's a bit of JavaScript or HTML for a web page, some SQL to create a database table or just a long bit of text for a tool tip, large strings are needed.

In the previous article, the included utility used a text file as a source, and wrote a code snippet that created and populated a string variable for you to use in your own code. This article takes a different approach by embedding the text file in your assembly and extracting it when needed.

The main advantage to this approach is maintainability. Since the text is not cut up by concatenation or StringBuilder.Append statements, it's easy to read and edit.

Using the code

Setting up the text file can be accomplished in 3 easy steps:

  1. Add a text file to your solution.
  2. Right-click on the file in the Solution Explorer and select "properties" to display the Properties window for the file.
  3. Change the "Build Action" to "Embedded Resource".

Once this is done, your file will be embedded in the assembly the next time you compile. Its contents can be read back using the following GetFromResources method and used to populate your string variable to be used in your application.

The code consists of a small method to extract the resource file as a string. Using reflection, a reference to the current assembly is created. Then, with the manifest reader method GetManifestResourceStream, the text file is read and returned as a string. That's it!

internal string GetFromResources(string resourceName)
   Assembly assem = this.GetType().Assembly;   
   using( Stream stream = assem.GetManifestResourceStream(resourceName) )   
       using( StreamReader reader = new StreamReader(stream) )         
         return reader.ReadToEnd();         
     catch(Exception e)      
       throw new Exception("Error retrieving from Resources. Tried '" 
                                + resourceName+"'\r\n"+e.ToString());      

Using the method to populate a string variable is accomplished in one line of code:

string quote = 
  new EmbeddedResourceTextReader().GetFromResources

Key Points to Remember:

  • The resourceName is case sensitive.
  • The resourceName must be the fully qualified name of the file: Default Namespace + folder name(s) + filename (with extension if applicable).
  • Your code file will need to have using statements for System.Reflection and System.IO. If you only change the text of an embedded resource file, you will need to rebuild your project because Visual Studio does not recognize the change as a code change worthy of recompiling.

I want to stress the last key point again. Several times I have changed only the embedded resource file and run my application only to see the old text returned. This can be pretty frustrating, especially if the expected change is buried deep in your application. If you remember that you need to rebuild your application after an embedded resource change, you'll be a much happier programmer.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Michael McKechney
Web Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 3 Pin
bigjoe11a20-Oct-10 6:50
memberbigjoe11a20-Oct-10 6:50 
Generalvery helpful Pin
shashankkadge12-Mar-10 2:37
membershashankkadge12-Mar-10 2:37 
GeneralMost Excellent Pin
campinas5-Feb-07 16:38
membercampinas5-Feb-07 16:38 
GeneralCool example! There is also an example in MSDN... Pin
SecondNature21-Dec-06 14:28
memberSecondNature21-Dec-06 14:28 
GeneralVery Good Pin
kignatov19-Apr-05 4:35
memberkignatov19-Apr-05 4:35 
Generalusing tatement Pin
Santiago Corredoira26-Sep-04 1:54
memberSantiago Corredoira26-Sep-04 1:54 
GeneralRe: using tatement Pin
Judah Himango29-Sep-04 11:36
memberJudah Himango29-Sep-04 11:36 
GeneralWhen the strings contain HTML... Pin
Anonymous3-Aug-04 19:29
sussAnonymous3-Aug-04 19:29 
GeneralVery Useful Pin
Colin Angus Mackay18-Jun-04 11:35
memberColin Angus Mackay18-Jun-04 11:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.1509028.1 | Last Updated 16 Jun 2004
Article Copyright 2004 by Michael McKechney
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid