Geeks With Blogs

News


Rodney Vinyard - .NET & SQL Developer When all is said and done, more will be said than done

 1) Add a file as an "embedded resource"

 

2) if the file is in a "subfolder" note the "folder discussion" below

 

3) client call to class

ResourceFileManager objResourceFileManager = new ResourceFileManager();
string x = objResourceFileManager.ReadResourceString("folder.resorceFile.txt");

4) class

using System;
using System.IO;
using System.Reflection;
 
public class ResourceFileManager
{
    public string ReadResourceString(string resourceFileName)
    {
        //value for our return value
        string resourceValue = string.Empty;
        try
        {
 
            //------------------------------------------------------------------
            //debug - this will actually help you locate callers resourceFileName name
            //eg is resx is in an ASP.NET folder "myFolder", -> "myFolder.resourceFileName"
            //------------------------------------------------------------------
            string[] names = this.GetType().Assembly.GetManifestResourceNames();
 
            //------------------------------------------------------------------
            //get current assembly name
            //------------------------------------------------------------------
            Assembly assembly = this.GetType().Assembly;
            string assemblyName = assembly.GetName().Name;
 
            //------------------------------------------------------------------
            //get resource value (text, string)
            //------------------------------------------------------------------
            StreamReader _textStreamReader
                = new StreamReader(assembly.GetManifestResourceStream(assemblyName + "." + resourceFileName));
 
            resourceValue = _textStreamReader.ReadToEnd();
 
 
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            resourceValue = string.Empty;
        }
        return resourceValue;
    }
}
 

 

 

Posted on Saturday, April 12, 2008 11:22 AM Resource Files resx | Back to top


Comments on this post: Get text from embedded resource files

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Rodney Vinyard | Powered by: GeeksWithBlogs.net