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();
    }        
}

2 Comments

  • 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;

  • Wim,

    Good point. Thanks for the feedback.

Comments have been disabled for this content.