views:

508

answers:

3

I have a html template that I want to retrieve from the resources file in a VS 2005 C# windows form application.

I've created a folder called /html/ within the project, which has one file in it called template.html.

I've added the file to my resources. I see its name just as template, and it's filepath is the fully qualified filename (c:/.../project/html/template.html). It is saved as TEXT rather than binary.

I've tried many methods to extract this file, but each time I get a null returned. What am I missing?

        Type t = GetType();
        Assembly a = Assembly.GetAssembly(t);
        string file = "html.template.html"; // I've tried template and template.html
        string resourceName = String.Concat(t.Namespace, ".", file);

        Stream str = a.GetManifestResourceStream(resourceName);

        if (str == null) // It fails here - str is always null.
        {
            throw new FileLoadException("Unrecoverable error. Template could not be found");
        }
        StreamReader sr = new StreamReader(str);
        htmlTemplate = sr.ReadToEnd();
+1  A: 

Have you tried looking in Reflector at your output assembly to verify that the resource name is actually what you expect it to be?

jerryjvl
A: 

I believe you're misnaming your resource when you attemp to retrieve it.

One thing you could do is to examine the generated assembly with Reflector and check the full name of your resource.

Paulo Santos
+1  A: 

Reflector helped in finding out what the problem is, thank you. This is what I needed to have:

string template = Properties.Resources.template;

Couldn't be easier really. All the other stuff above was completely unnecessary.

Josh Smeaton