Access strings resources from embedded .resx in dll?

橙三吉。 提交于 2020-03-06 04:14:20

问题


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 :)


回答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 :)




回答2:


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;


来源:https://stackoverflow.com/questions/17627903/access-strings-resources-from-embedded-resx-in-dll

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!