How can I access embedded resources in a C# project?

╄→гoц情女王★ 提交于 2019-11-27 06:57:23

问题


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.

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

So how can I embed resources in my application (normally), and access them, using Eclipse + Emonic + Nant to build it?

Thanks,

Paul.


回答1:


You should create a ResourceManager instance to open a resx file like this:

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



回答2:


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:

ResourceManager.GetString("String1", resourceCulture);


来源:https://stackoverflow.com/questions/6270079/how-can-i-access-embedded-resources-in-a-c-sharp-project

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