Microsoft KB Archive/820659

= How to use Visual Basic .NET or Visual Basic 2005 to create a resource file by using the ResourceWriter class and how to retrieve a resource file by using the ResourceManager class =

Article ID: 820659

Article Last Modified on 11/26/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



IN THIS TASK

 * SUMMARY
 * Requirements
 * Step-By-Step Example
 * Verify That It Works
 * REFERENCES



SUMMARY
This step-by-step article describes how to create a resource file by using the ResourceWriter class and how to retrieve a resource file by using the ResourceManager class.

The ResourceWriter class provides a default implementation of the IResourceWriter interface. You can specify resources as name and value pairs by using the AddResource method. To create a resource file, create a ResourceWriter instance with a unique file name and call AddResource at least one time. Call the Generate method to write the resource file to the disk, and then call the Close method to close the file. When you call the Close method, the resource file is implicitly generated, if it is required.

Note The resources might not be written to the resource file in the same order that they are added to the resource file.

The ResourceManager class examines culture-specific resources, provides resource fallback when a localized resource does not exist, and supports resource serialization. By using the ResourceManager class, a caller can access the resources for a particular culture by using the GetObject method and the GetString method. By default, these methods return the resource for the culture that is determined by the current cultural settings of the thread that made the call.

back to the top ===

Requirements ===

The following list outlines the recommended hardware, software, network infrastructure, and service packs that are required:
 * Microsoft Visual Studio .NET or Microsoft Visual Studio 2005

This article assumes that you are familiar with the following topics:
 * Microsoft Visual Basic .NET or Microsoft Visual Basic 2005
 * Microsoft Visual C# .NET or Microsoft Visual C# 2005

back to the top

Step-By-Step Example
 Start Visual Studio .NET 2002, Visual Studio .NET 2003, or Visual Studio 2005. On the File menu, point to New, and then click Project. Click Visual Basic Projects under Project Types, and then click Windows Application under Templates.

Note In Visual Studio 2005, click Visual Basic under Project Types.

By default, Form1 form is created. On the View menu, click Toolbox. Add a Button control to Form1. In the Properties window, set the Text property of the Button control to Create Resource. Double-click Create Resource.</li>  Add the following imports statement to the Form1.vb file: Imports System.Resources </li>  Add the following code in the Button1_Click event handler: ' Code to Create a Resource. Dim objImage As Image Dim strString As String Dim rsw As ResourceWriter

' objImage is the Image that will be added as a resource. objImage = Image.FromFile(&quot;c:\\Winter.jpg&quot;)

' strString is the string that will be added as a resource. strString = &quot;This is the string from resource&quot;

'Creates a resource writer instance to write to MyResource.resources. rsw = New ResourceWriter(&quot;MyResource.resources&quot;)

'Adds the image to the resource. ' &quot;MyImage&quot; is the name that the Image is identified as in the resource. rsw.AddResource(&quot;MyImage&quot;, objImage)

'Adds the string to the resource. ' &quot;MyText&quot; is the name that the string is identified as in the resource. rsw.AddResource(&quot;MyText&quot;, strString)

rsw.Close MessageBox.Show(&quot;Resource Is Created.....&quot;) Note Change the Image.FromFile (&quot;c:\\Winter.jpg&quot;) path based on the location of the image that you want to add as a resource. </li> Add a PictureBox control to Form1.</li> Add a Label control to Form1.</li> Add another Button control to Form1.</li> In the Properties window, set the Text property of the Button2 control to Use Resource.</li> Double-click Use Resource.</li>  Add the following code in the Button2_Click event handler: ' Code to retrieve the information from the resource. Dim myImage As Image Dim myString As String Dim rm As ResourceManager

' Create a Resource Manager instance. rm = ResourceManager.CreateFileBasedResourceManager(&quot;MyResource&quot;, &quot;.&quot;, Nothing)

' Retrieve the Image from MyResource by using the GetObject method. myImage = rm.GetObject(&quot;MyImage&quot;)

' Retrieves the string from MyResource. myString = rm.GetObject(&quot;MyText&quot;) PictureBox1.Image = myImage Label1.Text = myString </li> On the File menu, click Save to save the project.</li> On the Build menu, click Build Solution to build the project.</li> On the Debug menu, click Start to run the project.</li></ol>

back to the top

Verify That it Works

 * 1) When you run the project, Form1 appears.

Click Create Resource to create a resource file.

A message box confirms that you successfully created the resource file.
 * 1) Click Use Resource.
 * 2) Confirm that the image and the This is the string from resource text appears on the form.

back to the top

<div class="references_section">