none
How do I access resources as variables RRS feed

  • Question

  • I have a self-hosted web api console app. It includes a linked resource image that I can access as:

    Bitmap imgBitmap = Properties.Resources.img2

    However I want to access it as variable. I.e. the name of the resource will be provided as a variable containing the string: "img2"

    There are other questions on this issue, but they dont seem to work or have enough information for me to figure out how to do it. E.g.:

    http://social.msdn.microsoft.com/Forums/vstudio/en-US/c1a53013-ef11-4ad2-8bb5-e3a227c38604/pass-resource-name-as-variable?forum=netfxbcl

    So I tried:

    object obj = ResourceManager.GetObject("img2", Properties.Resources.resourceCulture);

    But that gave me the error:

    An object reference is required for the non-static field, method, or property 'System.Resources.ResourceManager.GetObject(string, System.Globalization.CultureInfo)' ...

    So I tried to instantiate it (but I am unsure how exactly how to specify this) (I have tried various combinations) ...

    var TheResourceManager =
        new System.Resources.ResourceManager(
            "Properties.Resources",
            typeof(Properties.Resources).Assembly);

    Bitmap imgBitmap = (Bitmap)TheResourceManager.GetObject("img2");

    And that gave me the run time error...

    "Could not find any resources appropriate for the specified culture or the neutral culture.  Make sure "Properties.Resources.resources" was correctly embedded or linked into assembly "OwinWebApi2" at compile time, or that all the satellite assemblies required are loadable and fully signed."


    • Edited by spiderplant0 Wednesday, February 5, 2014 5:16 PM
    Wednesday, February 5, 2014 5:13 PM

Answers

  • Hi, please try this:

    object obj = Properties.Resources.ResourceManager.GetObject("img2", Properties.Resources.Culture);
    Bitmap bmp = (Bitmap)obj;

    • Marked as answer by spiderplant0 Thursday, February 6, 2014 12:17 AM
    Wednesday, February 5, 2014 5:35 PM