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 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.
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
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:
back to the top Step-By-Step Example
- Microsoft Visual Basic .NET or Microsoft Visual Basic 2005
- Microsoft Visual C# .NET or Microsoft Visual C# 2005
back to the topVerify That it Works
- 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.
- 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 resource.
' Code to Create a Resource. Dim objImage As ImageDim strString As StringDim 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.....")
- 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 Resource.
- 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 ImageDim myString As StringDim 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 = myImageLabel1.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.
back to the top
- 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.
- Click Use Resource.
- Confirm that the image and the This is the string from resource text appears on the form.