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.
public void ExtractAndSaveToFile(string resourceFileName,string destinationFileName)
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");
rcManager.LoadImage("MyImage") or rcManager.LoadImage("myImage.jpg");
History
- 29th January, 2006: Initial post