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 translations Article translations
Article ID: 820659 - View products that this article applies to.
This article has been archived. It is offered "as is" and will no longer be updated.
Expand all | Collapse all

On This Page

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.



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

Step-By-Step Example

  1. Start Visual Studio .NET 2002, Visual Studio .NET 2003, or Visual Studio 2005.
  2. On the File menu, point to New, and then click Project.
  3. 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.
  4. On the View menu, click Toolbox.
  5. Add a Button control to Form1.
  6. In the Properties window, set the Text property of the Button control to Create Resource.
  7. Double-click Create Resource.
  8. Add the following imports statement to the Form1.vb file:
    Imports System.Resources
  9. 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("c:\\Winter.jpg")
    
    ' strString is the string that will be added as a resource.
    strString = "This is the string from resource"
    
    'Creates a resource writer instance to write to MyResource.resources.
    rsw = New ResourceWriter("MyResource.resources")
    
    'Adds the image to the resource.
    ' "MyImage" is the name that the Image is identified as in the resource.
    rsw.AddResource("MyImage", objImage)
    
    'Adds the string to the resource.
    ' "MyText" is the name that the string is identified as in the resource.
    rsw.AddResource("MyText", strString)
    
    rsw.Close()
    MessageBox.Show("Resource Is Created.....")
    Note Change the Image.FromFile ("c:\\Winter.jpg") path based on the location of the image that you want to add as a resource.
  10. Add a PictureBox control to Form1.
  11. Add a Label control to Form1.
  12. Add another Button control to Form1.
  13. In the Properties window, set the Text property of the Button2 control to Use Resource.
  14. Double-click Use Resource.
  15. 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("MyResource", ".", Nothing)
    
    ' Retrieve the Image from MyResource by using the GetObject method.
    myImage = rm.GetObject("MyImage")
    
    ' Retrieves the string from MyResource.
    myString = rm.GetObject("MyText")
    PictureBox1.Image = myImage
    Label1.Text = myString
  16. On the File menu, click Save to save the project.
  17. On the Build menu, click Build Solution to build the project.
  18. On the Debug menu, click Start to run the project.

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.
  2. Click Use Resource.
  3. Confirm that the image and the This is the string from resource text appears on the form.

REFERENCES

For more information, visit the following Microsoft Developer Network (MSDN) Web sites:
ResourceManager Class
http://msdn2.microsoft.com/en-us/library/system.resources.resourcemanager(vs.71).aspx
Retrieving Resources in .Resources Files
http://msdn2.microsoft.com/en-us/library/khyt7e7y(vs.71).aspx
Resources in Applications
http://msdn2.microsoft.com/en-us/library/f45fce5x(vs.71).aspx

Properties

Article ID: 820659 - Last Review: February 27, 2014 - Revision: 2.6
APPLIES TO
  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
Keywords: 
kbnosurvey kbarchive kbvs2005applies kbvs2005swept kbstring kbresource kbbitmap kbhowtomaster KB820659

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com