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

Paul picture Paul · Jun 7, 2011 · Viewed 11.1k times · Source

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.

Answer

Jan-Peter Vos picture Jan-Peter Vos · Jun 7, 2011

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