Click here to Skip to main content
15,881,881 members
Articles / Programming Languages / C#
Article

Using Resources In Windows Applications

Rate me:
Please Sign up or sign in to vote.
2.94/5 (18 votes)
29 Jan 2006CPOL3 min read 83.2K   2.5K   30   8
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.

C#
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.

C#
public Resourcer(Type containedClass)

Method Specifications

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

C#
public Image LoadImage(string imageFileName)

Loads an embedded image file from a DLL or executable.

C#
public Icon LoadIcon(string iconFileName)

Loads an embedded icon file from a DLL or executable.

C#
public System.Xml.XmlDocument LoadXML(string xmlFileName)

Loads an embedded XML file from a DLL or executable.

C#
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.

C#
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:

C#
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)



Comments and Discussions

 
GeneralUpdate Embedded Resource file dynamically Pin
Member 183766121-Dec-07 1:27
Member 183766121-Dec-07 1:27 
QuestionSome questions Pin
Dan Suthar20-Nov-07 0:23
professionalDan Suthar20-Nov-07 0:23 
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 ?

Thanks,
Dan
GeneralWhy Pin
guycox22-Mar-07 4:45
guycox22-Mar-07 4:45 
QuestionChanging icon of an EXE programatically Pin
ciM2pHat4U31-Jul-06 17:49
ciM2pHat4U31-Jul-06 17:49 
AnswerRe: Changing icon of an EXE programatically Pin
neohacker1025-Apr-07 22:24
neohacker1025-Apr-07 22:24 
QuestionWhat about .Resx resource?? Pin
fengtian8-Jun-06 9:46
fengtian8-Jun-06 9:46 
AnswerRe: What about .Resx resource?? Pin
User 20930738-Jun-06 20:13
User 20930738-Jun-06 20:13 
GeneralRe: What about .Resx resource?? Pin
fengtian12-Jun-06 13:53
fengtian12-Jun-06 13:53 

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

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