How To: Return Embedded Resource Content As String

Here is a utility method for returning any embedded resource content as a string:

public static partial class Tools
{
    public static string GetEmbeddedContent(string resourceName)
    {
        Stream resourceStream =
            Assembly.GetAssembly(typeof(Tools)).GetManifestResourceStream(resourceName);
        StreamReader reader = new StreamReader(resourceStream);
        return reader.ReadToEnd();
    }        
}
Published Saturday, July 07, 2007 1:04 PM by Palermo4
Filed under: , ,

Comments

# re: How To: Return Embedded Resource Content As String

Monday, July 09, 2007 8:39 AM by Wim Hollebrandse

You really should close the StreamReader before going out of scope. ReadToEnd() does not explicitly close or dispose of the StreamReader object.

Alternatively and better still (than calling .Close()) - use the using block as the StreamReader's underlying TextReader class implements IDisposable.

string content=null;

using (StreamReader reader=new StreamReader(resourceStream))

{

 content = reader.ReadToEnd();

}

return content;

# re: How To: Return Embedded Resource Content As String

Monday, July 09, 2007 6:17 PM by Palermo4

Wim,

Good point.  Thanks for the feedback.

Leave a Comment

(required) 
(required) 
(optional)
(required)