1. Computing

VB.NET Resource Files

Want to include a graphic, string, or audio file in your actual project assembly

By

Updated January 18, 2009

After Visual Basic students learn all about loops and conditional statements and subroutines, one of the next things that they often ask about is, "How do I add a bitmap, a wav file, a custom cursor or some other special effect?" One answer is resource files. When you add a resource file to your project, it's integrated directly in for maximum execution speed and minimum hassle packaging and deploying your application.

Note - About Visual Basic hasn't forgotten out VB 6 readers. A companion article to this one, but focusing on VB 6, can be found by clicking this link.

This isn't the only way to use files in a VB project, but it has real advantages. For example, you could include a bitmap in a PictureBox control or use the mciSendString Win32 API. ("MCI" is a prefix for "Multimedia Command String".)

Microsoft defines a resource this way:

"A resource is any nonexecutable data that is logically deployed with an application."

The easiest way to manage resource files in your project is to selecting the Resources tab in the project properties. You can bring this up by double-clicking My Project in Solution Explorer or your project Properties under the Project menu item.

The resource types that are supported in the Resource Editor are:

  • Strings
  • Images (PNG, BMP, GIF, JPEG, and TIFF are supported!)
  • Icons
  • Audio
  • Files
  • Other

Using resource files adds another advantage: better globalization. (Microsoft has clearly heard the jingling clink of rupees, lira, yen, and krona in .NET.) Resources are normally included into your main assembly, but .NET also lets you package resources into satellite assemblies. This lets you accomplish better globalization because you can include just those satellite assemblies when they're needed. Microsoft has given each language dialect a code. For example, the American dialect of English is indicated by the string "en-US", and the Swiss dialect of French is indicated by "fr-CH". These codes identify the satellite assemblies that contain culture specific resource files, for example: "en-AU.resx" for Australian English. When an application runs, Windows will automatically use the resources contained in the satellite assembly with the culture determined from Windows settings.

Since resources are a property of the solution in VB.NET, you access them just like other properties: by name, using the My.Resources object. To make this more clear, let's build an application to display icons for Aristotle's four elements: Air, Earth, Fire and Water.

Step 1 is to add the icons. Select the Resources tab from your project Properties. You can add icons by either choosing Add Existing File... from the Add Resources drop down menu, or just drag and drop from a Windows Explorer window.

After a resource has been added, the new code looks like this:

Private Sub RadioButton1_CheckedChanged( ...
   Handles MyBase.Load
   Button1.Image = My.Resources.EARTH.ToBitmap
   Button1.Text = "Earth"
End Sub

Although I do my best to keep these articles at a level where VB.NET Express can always be used, there is another way to use resources that isn't supported in Express. But if you're using Visual Studio, you can embed them directly in your project assembly. These steps will add an image directly into to your project.

  • Right-click the project in the Solution Explorer, click Add, then click Add Existing Item.
  • Browse to your image file and click Open.
  • Display the properties for the image that was just added.
  • Set the Build Action property to Embedded Resource.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

You can then use the bitmap directly in code like this (the bitmap was the third one - index number 2 - in the assembly).

Dim res() As String = GetType(Form1).Assembly.GetManifestResourceNames()
PictureBox1.Image = New System.Drawing.Bitmap( _
   GetType(Form1).Assembly.GetManifestResourceStream(res(2)))

Although these resources are embedded as binary data directly in the main assembly (or in satellite assembly files) when you Build your project, in Visual Studio, they're referenced by an XML-based file format that use the extension .resx. For example, here's a snippet from the .resx file we just created:

<assembly alias="System.Windows.Forms" name="System.Windows.Forms,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
   <data name="AIR"
   type="System.Resources.ResXFileRef,
   System.Windows.Forms">
      <value>..\Resources\CLOUD.ICO;System.Drawing.Icon,
      System.Drawing, Version=2.0.0.0,
      Culture=neutral,
      PublicKeyToken=b03f5f7f11d50a3a</value>
   </data>

You can write directly to these files at run time using the ResXResourceWriter object in the System.Resources namespace, even adding the actual binary information in a value XML element.

<value>
   AAEAAAD/////AQAAAAAAAAAMAgAAADtTeX…
</value>

ResXResourceReader, in turn, will read .resx files.

Because they're just text XML files, a .resx file can't be used directly by a .NET Framework application. It has to be converted to a binary ".resources" file adding it to your application. This job is accomplished by a utililty program named Resgen.exe. You might want to do this to create the satellite assemblies for the globalization mentioned earlier. It can also convert binary .resources files back into XML .resx files but only string resources get converted correctly.

And DOS will never die! You have to run resgen.exe from a Command Prompt.

  1. About.com
  2. Computing
  3. Visual Basic
  4. Using VB.NET
  5. Using Resources in VB.NET

©2014 About.com. All rights reserved.