Using Resource File on DotNet

[http://www.jelovic.com/articles/resources_in_visual_studio.htm]

Using Resources in Visual Studio .NET

by Dejan Jelovic

Concepts

Here's how things break down:

In Visual Studio .NET, many resources are no longer specified using .RC files. Instead, the form editor directly manipulates your VB or C# code when you edit the forms visually..

We still need other types of resources, though. We need graphic resources like bitmaps and icons, as well as locale-specific string resources. These are typically stored in one or more .RESX files that make up a Visual Studio project.

In order to use resources from your code, you'll need to know how to:

  1. Create RESX files.
  2. Add bitmaps and other types of resources to the RESX files.
  3. Load these resources from your code.

So let's get started.

1. Create RESX Files

Visual Studio .NET stores resources like bitmaps, icons and locale-specific strings in files with the extension .RESX. These are actually XML files in which binary resources are base64-encoded.

When you compile a project the compiler will compile these files into binary files with the .resource extension, and then the linker will put these into the final EXE or DLL.

Adding a new RESX file to a project is simple. Simply right-click on a project in the Solution Explorer, select Add/Add New Resource, and add a new "Assembly Resource File".

2. Adding Resources to RESX Files

Here's the funny thing: Visual Studio doesn't seem to have any tools for visually editing resources in a RESX file.

Fortunately, a Lutz Roeder has created a tool named Resourcer that you can use to edit them.

To add a bitmap to a RESX file:

  1. Run Resourcer.
  2. Open the RESX files that you have added to the Visual Studio project in Resourcer.
  3. Select Add/Add File.
  4. In the dialog that pops up, type in the file name of the bitmap file and a symbolic name by which the content of that bitmap will be known inside the RESX file.
  5. When you click OK, the content of the bitmap will be base64 encoded and placed in the RESX files under the name that you have specified.
  6. Save the RESX file by selecting File/Save.

And you are done. BTW, you don't need to keep the bitmap file on the disk any more, as the RESX file contains its complete copy.

3. Using Resources from Your Code

Here's how to load the bitmap from the resources at run time:

The first thing you should know is under which name Visual Studio has placed your RESX file into the resulting EXE.

Each Visual Studio project has a namespace name associated with it. If you right-click on a project file in Solution Explorer, then select Properties, a dialog that pops up lets you select a "Default Namespace". Visual Studio will bundle the resources into the resulting EXE under the name made up from the default namespace and the name of the RESX file.

Say your default namespace is MyCompany.MyProject and the RESX file is named SomeResources. Those resources will be bundled inside the resulting executable under the name MyCompany.MyProject.SomeResources.

To access those resources from your code, you'll need to create an instance of ResourceManager:

ResourceManager resourceManager = new ResourceManager ("MyCompany.MyProject.SomeResources", GetType ().Assembly);

Then, to obtain bitmap, you will use the symbolic name that you have specified when you have added the bitmap to the RESX file using Resourcer:

Bitmap image = (Bitmap)resourceManager.GetObject ("MyBitmapName");

If you attempt to create a resource manager using a wrong name, or you specify a wrong name when calling ResourceManager.GetObject, the framework will throw theMissingManifestResourceException.

That's it. Have fun now.

posted @ 2010-03-18 14:06  能巴  阅读(381)  评论(0编辑  收藏  举报