从 dll 中嵌入的 .resx 访问字符串资源? [英] Access strings resources from embedded .resx in dll?

查看:56
本文介绍了从 dll 中嵌入的 .resx 访问字符串资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对资源文件完全陌生,但我需要将我的应用程序部署为单击一次,这似乎忽略了我的所有外部文件(图像、.ini 文件等...)-与其花时间去弄清楚,我想我应该学会如何正确使用资源文件.

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.

通过SO搜索后,我找到了很多代码并且我已经构建了我的资源文件.到目前为止它只包含字符串,我认为它会更简单!?唉……

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...

所以我有我的 DLL (ValhallaLib.dll) 和这两个用于处理资源的函数(这些函数保存在一个静态 Helper 类中,我所有的随机但有用的函数都存在于其中):

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");

现在,除了得到 MissingManifestException 之外……我的问题是,我不知道(并且似乎无法找到直接的答案!!) resName 应该是什么.我的资源文件被称为:StringResource.resx - 然而,它不是说 Assembly.ValhallaLib.StringResource.

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?

更新我已经尝试过 global::ValhallaLib.StringResource - 但这也不是我真正想要的.

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

更新 2解决了.我设法让它与它一起工作:

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);
    }

我不知道为什么我觉得这太复杂了.可能是因为我睡了大约 2 小时.

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

推荐答案

facepalm 我想通了.我可以使用

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);
    }

忽略我:)

这篇关于从 dll 中嵌入的 .resx 访问字符串资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆