eclipse 如何访问 C# 项目中的嵌入式资源?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6270079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I access embedded resources in a C# project?
提问by Paul
Most of the threads I've read about this question answer that you just have to access them like this:
我读过的关于这个问题答案的大多数线程,你只需要像这样访问它们:
<MyProjectNamespace>.Properties.Resources.<MyResourceName>
But at the build process I have this message:
但是在构建过程中,我收到了以下消息:
<MyProjectNamespace> does not contain a definition for 'Properties'
It seems that the class 'Properties' is normally auto-generated by the IDE when you add resources to your project.
当您向项目添加资源时,IDE 似乎通常会自动生成“属性”类。
The fact is that I'm working on Eclipse and I don't really know how to add resources, but I managed to generate a .resx file from Visual Studio (which I use to design my Windows form) that I added to Nant .build file as a 'resource', along with my bitmaps. Then it indeed embed the resources, but I can't access them...
事实上,我正在 Eclipse 上工作,我真的不知道如何添加资源,但我设法从添加到 Nant 的 Visual Studio(我用来设计我的 Windows 表单)生成了一个 .resx 文件。构建文件作为“资源”,以及我的位图。然后它确实嵌入了资源,但我无法访问它们......
So how can I embed resources in my application (normally), and access them, using Eclipse + Emonic + Nant to build it?
那么如何在我的应用程序中嵌入资源(通常)并访问它们,使用 Eclipse + Emonic + Nant 来构建它?
Thanks,
谢谢,
Paul.
保罗。
回答by Jan-Peter Vos
You should create a ResourceManager
instance to open a resx file like this:
您应该创建一个ResourceManager
实例来打开这样的 resx 文件:
ResourceManager resources = new ResourceManager("<MyProjectNamespace>.Properties.Resources", typeof(<any type in the assembly>).Assembly);
string myString = resources.GetString("myString");
Bitmap myBitmap = resources.GetObject("myBitmap") as Bitmap;
If they are the resources of a form you can also get them as following:
如果它们是表单的资源,您还可以通过以下方式获取它们:
ResourceManager resources = new ResourceManager(typeof(Form1));
回答by therealmitchconnors
Try using ResourceManager in the System.Resources Namespace. Obviously, it depends a bit on what you are trying to retrieve, but mine looks like this:
尝试在 System.Resources 命名空间中使用 ResourceManager。显然,这在一定程度上取决于您要检索的内容,但我的看起来像这样:
ResourceManager.GetString("String1", resourceCulture);