Click here to Skip to main content
Click here to Skip to main content

Using Resources In Windows Applications

, 29 Jan 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
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.
Sample Image - ScreenShot.jpg

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:

  1. Non-expert users can't change or remove the vital needed files from the application directory and cause the application to crash.
  2. 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:

  1. Create a Windows application or class library project.
  2. Right click on the selected project in Solution Explorer and select Add->Existing Item.
  3. Find the resource file you want to add to the project and press ok.
  4. Right click on the added resource file in solution explorer and click properties.
  5. 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"); // Will Work successfully. 
rcManager.LoadImage("MyImage") or rcManager.LoadImage("myImage.jpg"); //Will Fail.

History

  • 29th January, 2006: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

ALLEXY
Software Developer (Senior)
Australia Australia
MCPD
MCTS: ASP.NET Applications
MCTS: ADO.NET Applications
MCTS: Windows Forms Applications
MCTS: Windows Communication Foundation Applications
MCPD: Enterprise Application Developer

Comments and Discussions

 
GeneralUpdate Embedded Resource file dynamically PinmemberMember 183766121-Dec-07 2:27 
QuestionSome questions PinmemberDan'M20-Nov-07 1:23 
GeneralWhy Pinmemberguycox22-Mar-07 5:45 
QuestionChanging icon of an EXE programatically PinmemberciM2pHat4U31-Jul-06 18:49 
AnswerRe: Changing icon of an EXE programatically Pinmemberneohacker1025-Apr-07 23:24 
QuestionWhat about .Resx resource?? Pinmemberfengtian8-Jun-06 10:46 
AnswerRe: What about .Resx resource?? Pinmemberxironix8-Jun-06 21:13 
GeneralRe: What about .Resx resource?? Pinmemberfengtian12-Jun-06 14:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141216.1 | Last Updated 29 Jan 2006
Article Copyright 2006 by ALLEXY
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid