0

I'm completely new to resource files, but there is a need for me to deploy my application as click-once, and that seems to ignore all of my external files (images, .ini files etc...) - Rather than spend time trying to figure it out, I thought I'd learn how to use Resource files properly.

After searching through SO, I've found much code and I've built my resource file. So far it only contains strings, which I thought would be simpler!? Alas...

So I've got my DLL (ValhallaLib.dll) and these two functions for dealing with resources (these functions are held within a static Helper Class, where all my random but useful functions live):

    public static Bitmap getImageByName(string imgName)
    {
        System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly();
        string resName = asm.GetName().Name + ".Properties.Resources";
        var rm = new System.Resources.ResourceManager(resName, asm);

        return (Bitmap)rm.GetObject(imgName);
    }


    public static string getStringByName(string var)
    {
        System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly();
        string resName = asm.GetName().Name + ".Properties.Resources";
        var rm = new ResourceManager(resName, asm);

        return rm.GetString(var);
    }  

And I'm attempting to call them with a simple:

CHelpers.getStringByName("db_host_address");

Now, other than getting a MissingManifestException... My problem is, that I have no idea (and can't seem to find a straight answer!!) what the resName should be. My resources file is called: StringResource.resx - Yet, its not a case of saying Assembly.ValhallaLib.StringResource.

Can anyone offer some guidance, please?

Update I've tried global::ValhallaLib.StringResource - but that's not really what I'm after either.

Update 2 Solved it. I've managed to get it to work with:

    public static string getStringByName(string var)
    {
        System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly();
        string resName = asm.GetName().Name + ".Properties.Resources";
        var rm = new ResourceManager("ValhallaLib.StringResource", asm);

        return rm.GetString(var);
    }

I don't know why I found that so overly complicated. Possibly because I've had about 2 hrs sleep.

Cheers to those who tried though :)

laminatefish
  • 5,197
  • 5
  • 38
  • 70
  • 1
    I would like to add that this is redundant in your code `string resName = asm.GetName().Name + ".Properties.Resources";`, I don't see it's used anywhere in the scope of method `getStringByName()`, it should be removed. – King King Jul 13 '13 at 07:42
  • Yes, you're absolutely correct and it's since been removed :) Thanks! – laminatefish Jul 13 '13 at 08:16

2 Answers2

1

facepalm I've figured it out. I can access the resources file using

    public static string getStringByName(string var)
    {
        System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly();
        string resName = asm.GetName().Name + ".Properties.Resources";
        var rm = new ResourceManager("ValhallaLib.StringResource", asm);

        return rm.GetString(var);
    }

Ignore me :)

laminatefish
  • 5,197
  • 5
  • 38
  • 70
0

resName is a base name for resources. In your case you can simple use full type name of resource class.

var resName = typeof(StringResource).FullName;
SergeyIL
  • 575
  • 5
  • 11