Using Resources In Windows Applications

This is a resource manager class library. You can put any type of resource such as image, icon, Wave, MIDI and ... in the executable of your application and then load it from the EXE or DLL file by using the methods of this library.

Introduction

This is a resource manager class library. You can put any type of resource such as image, icon, Wave, MIDI and so on in the executable of your application and then load it from the EXE or DLL file by using the methods of this library.

What is a Resource?

A resource can be any type of file that you can put it in an assembly at design time and get it out at runtime. Any file can be a resource such as Icon, Bitmap, Wave, Avi, XML and so on.

What is the Advantage of Resources?

With compressing resource files inside the main executable or a separate DLL file, you can hide any extra files that are used in the application from the user. This has some benefits:

Non-expert users can't change or remove the vital needed files from the application directory and cause the application to crash.

You can put everything in a single file and avoid the application directory to be bustled.

How to Add Resources

You can use the Resource Designer to add or edit resources for your project. In Visual Studio .NET IDE, you can do the following steps to add an existing resource at design time to your project:

Create a Windows application or class library project.

Right click on the selected project in Solution Explorer and select Add->Existing Item.

Find the resource file you want to add to the project and press ok.

Right click on the added resource file in solution explorer and click properties.

In the Properties tab of selected file, change the 'Build Action' from 'Content' to 'Embedded Resource'.

The last step is for compressing the resource file into the assembly and creating a single file.

How this Library Works

After adding the resource file(s) to an assembly, for using that file(s), we need to load the assembly at runtime and then fetch the resource(s) out from it. Microsoft .NET has many useful functions for working with assemblies at runtime. Yes, the System.Reflection.Assembly is our friend. The Proshot.ResourceManager library uses Assembly functions to load the target assemblies and pull the resources out from them.

You can load an assembly module at runtime and do many actions with it. This class has three constructors.

The first constructor expects a string as its argument that specifies the DLL or executable file path and name that your desired resource is compressed in that. Use this constructor if the resource file(s) is in an external EXE or DLL other than current executable or DLL.

public Resourcer(string containerFile)

The second constructor expects a LoadMethod - that is an enumeration as its argument. This enum has two members:

FromCallingCode: Loads the assembly that contains the method that called the currently executing code.

FromCurrentCode: Loads the assembly that contains the code that is currently executing.

After creating an instance, you can use any of the provided methods to load embedded resources from the loaded assembly. In the sample program, you can see all details.

With the last constructor, you can load an assembly in which the specified class is defined.

public Resourcer(Type containedClass)

Method Specifications

This library has some methods for fetching resources at runtime. Here is some brief explanation of these methods:

public Image LoadImage(string imageFileName)

Loads an embedded image file from a DLL or executable.

public Icon LoadIcon(string iconFileName)

Loads an embedded icon file from a DLL or executable.

public System.Xml.XmlDocument LoadXML(string xmlFileName)

Loads an embedded XML file from a DLL or executable.

public Stream GetResourceStream(string resourceFileName)

Loads an embedded resource file from a DLL or executable. You can use this method to extract any embedded resource types. For example AVI or WAV files.

Loads an embedded resource file from a DLL or executable and saves it to a separate new file.

Final Important Note

The name of resources is Case Sensitive in Microsoft Windows. In addition, you should give the full 'filename.extention' of the resource when you want to load the resource at runtime. For example, if you added a resource named MyImage.jpg, you should call the LoadImage function exactly with this name in true cases as an example:

rcManager.LoadImage("MyImage.jpg"); // Will Work successfully.
rcManager.LoadImage("MyImage") or rcManager.LoadImage("myImage.jpg"); //Will Fail.

Hi,
I am using your "ExtractAndSaveToFile" to extract some files from my project. It works fine. But it always extract file on the same location where the project exe is running. (Like in the bin folder). I want to change that path. Do you know how to do that ? And secondly, I want to show a progress bar during all this process. Is it possible ?

Is there a way to extract the icon from one EXE file, and then embed that into another EXE file programatically? For example, with C# is there a way to programatically get the icon from calc.exe then set or embed that into notepad.exe, so that notepad.exe will have the same windows icon as calc.exe?

Dear Fengtian,
Why do you want to fetch resources placed in the imagelist at runtime!? If you added the images at design time you dont need to load them dynamically and if you have images at runtime and you can add them dynamically in code,again,you dont need to load them at runtime in this manner !
Please send me if there is another question.
Thanx,
XironiX

Thank you very much for your timely answer. Maybe I didn't understand your code even it is very helpful to me What I want is to load multiple resource assemblies on-the-fly, which might have imagelists included. To make it shorter, I don't known which resource assembly will be loaded at what time.