Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 587
Paul Avatar asked Jun 07 '11 18:06

Paul


People also ask

What is embedded resource in C#?

Embedded files are called as Embedded Resources and these files can be accessed at runtime using the Assembly class of the System. Reflection namespace. Any file within the project can be made into an embedded file.

How do you add an embedded resource?

Open Solution Explorer add files you want to embed. Right click on the files then click on Properties . In Properties window and change Build Action to Embedded Resource . After that you should write the embedded resources to file in order to be able to run it.

What is a manifest resource?

A manifest resource is a resource (such as an image file) that is embedded in the assembly at compile time. For more information about manifest resources, see Microsoft .


1 Answers

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));
like image 53
Jan-Peter Vos Avatar answered Sep 19 '22 15:09

Jan-Peter Vos