App_GlobalResources List All Strings

N

nils.harrison

I would like to list/enumerate all the strings in my resources files
in an ASP.NET 2.0 web application, for my own personal convenience.

I just want to be able to display the information

key1 string1
key2 string2

Nothing fancy.

I've looked at ResourceReader/ResourceManager but I'm not making any
headway.
 
G

Guest

Howdy,

ResourceSet resources = Resources.Resource.ResourceManager.GetResourceSet(
new System.Globalization.CultureInfo("en"), false, true);

IDictionaryEnumerator enumerator = resources.GetEnumerator();

while (enumerator.MoveNext())
{
string key = (string) enumerator.Key;
string value = (string) enumerator.Value;
}

Hope this helps
 
N

nils.harrison

Hey Milosz,

I had been using similar code to what you have just suggested.
Unfortunately I can't seem to get a reference to the Resource file,
so:

ResourceSet resources =
Resources.Resource.ResourceManager.GetResourceSet(

resources is null :/

My workaround, which is a little ugly is:

String pathName = Server.MapPath("App_GlobalResources");
ResXResourceSet resFile = new ResXResourceSet(pathName + "\
\Time.resx");
IDictionaryEnumerator idenum = resFile.GetEnumerator();


Incidentally I tried replacing:
ResourceSet resources =
Resources.Resource.ResourceManager.GetResourceSet(
with:
ResourceSet resources =
Resources.Time.ResourceManager.GetResourceSet(
 
G

Guest

You could also use:


Type type = typeof(System.Web.Compilation.BuildManager);

PropertyInfo propertyInfo = type.GetProperty("AppResourcesAssembly",
BindingFlags.Static |
BindingFlags.GetField |
BindingFlags.NonPublic);

Assembly assembly = (Assembly) propertyInfo.GetValue(null, null);

string[] names = assembly.GetManifestResourceNames();
string resource = names[0];
string baseName = resource.Substring(0, resource.LastIndexOf('.'));

ResourceManager manager = new ResourceManager(baseName, assembly);

ResourceSet resources = manager.GetResourceSet(
System.Globalization.CultureInfo.CurrentCulture, true, true);

IDictionaryEnumerator enumerator = resources.GetEnumerator();

while (enumerator.MoveNext())
{
string key = (string) enumerator.Key;
string value = (string)enumerator.Value;
}

Regards
 
N

nils.harrison

You've been very helpful Milosz, thanks for your time. I'll use your
advice and get back to my task :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top