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 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
method to close the file.
When you call the Close
method, the resource file is implicitly generated, if it is
The resources might not be written to the resource file in the same
order that they are added to the resource file.
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.
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
- Microsoft Visual Basic .NET or Microsoft Visual Basic 2005
- Microsoft Visual C# .NET or Microsoft Visual C# 2005
- 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
- Add a Button control to Form1.
- In the Properties window, set the Text
property of the Button control to Create
- Double-click Create Resource.
- Add the following imports statement to the Form1.vb file:
- Add the following code in the Button1_Click event handler:
Note Change the Image.FromFile ("c:\\Winter.jpg") path based on the
location of the image that you want to add as a
' 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.
'Adds the string to the resource.
' "MyText" is the name that the string is identified as in the resource.
MessageBox.Show("Resource Is Created.....")
- Add a PictureBox control to Form1.
- Add a
Label control to Form1.
- Add another Button control to Form1.
- In the Properties window, set the Text
property of the Button2 control to Use
- Double-click Use Resource.
- 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
- On the File menu, click Save
to save the project.
- On the Build menu, click Build
Solution to build the project.
- On the Debug menu, click
Start to run the project.
Verify That it Works
- When you run the project, Form1 appears.
Click Create Resource to create a resource
A message box confirms that you successfully created the
- Click Use Resource.
- Confirm that the image and the This is the string
from resource text appears on the form.
For more information, visit the following Microsoft
Developer Network (MSDN) Web sites:
Article ID: 820659 - Last Review: November 26, 2007 - Revision: 2.6
- Microsoft Visual Basic 2005
- Microsoft Visual Basic .NET 2003 Standard Edition
- Microsoft Visual Basic .NET 2002 Standard Edition
|kbvs2005applies kbvs2005swept kbstring kbresource kbbitmap kbhowtomaster KB820659|