Get all strings from resourcemanager
Asked Answered
D

1

15

I need to write a program, that reads all string resources from dll and insert them into some table. I have the method, that reads resources:

    private static IEnumerable<KeyValuePair<string,string>> getAllResources(ResourceManager resourceManager, 
        Language language)
    {

        ResourceSet resourceSet = resourceManager.GetResourceSet(getCulture(language), true, true);

        IDictionaryEnumerator dictNumerator = resourceSet.GetEnumerator();

        // Get all string resources
        while (dictNumerator.MoveNext())
        {
            // Only string resources
            if (dictNumerator.Value is string)
            {
                var key = (string)dictNumerator.Key;
                var value = (string)dictNumerator.Value;
                yield return new KeyValuePair<string, string>(key, value);
            }
        }
    }

But when I started using it, I noticed that it also reads the resources, that added like a file (reads file content)

How can I ignore resources that are added as a "file", and read only strings?

Deduce answered 30/6, 2011 at 7:57 Comment(2)
Wouldn't it be possible to use as casting on the value?Hipbone
I can use "as" instead of "is", but it's not solving my problemDeduce
A
3

There is no way of doing that. Have a look to the resource section of you assembly through Reflector, for instance. Your text file is saved as String. There is no difference between String value and Text File value.

Binary files, however, won't be a problem, as for binary file types you'll have byte[] as value and not string.

Autoclave answered 30/6, 2011 at 8:48 Comment(2)
yes, binary files is not a problem. Problem only with text files. But when i'm watching the resources in Visual Studio 2010, they are sorted by type: "Strings"\"Files"\"Images"\"Icons". How they are doing that?Deduce
These are resource representation of Visual Studio. It uses files and pictures and icons locations to load them as resources into assembly. After that, you have only assembly with bitmap, string or byte[] data.Autoclave

© 2022 - 2024 — McMap. All rights reserved.