Step-by-Step Demonstration
To add embedded resources to your project, you must first add the files as part of your project. After you have added the files to your project, you can access and display the resources through the System.Reflection namespace.Add Embedded Resources
To add a text file and an image file to your project as embedded resources, follow these steps:- Create a new Windows Application project for this demonstration. This form is used to display the resources that are accessed from the executing assembly during run time.
- Right-click your project name, click Add, and then click Add New Item.
- In the New Item dialog box, select Text File from the menu, and name the file MyTextFile.txt. When the file opens in the integrated development environment (IDE), add some text, and then close the file.
- Repeat steps 1 and 2 to add a bitmap image to your project, but instead of selecting Text File as the new item type, select Bitmap File, and then change the file name to MyImage.bmp. When the new image is opened in the IDE, draw something on the image, and then close the file.
- Right-click either the text file or the bitmap, and then select Properties.
- In the Properties dialog box, locate the Build Action property. By default, this property is set to Content. Click the property and change the Build Action property to Embedded Resource.
- Repeat steps 4 and 5 for the other file.
NOTE: The resource file names are case-sensitive. When you access the resources, you must use the exact spelling and case of the file name. If you do not use the exact spelling and case of the file name, the method call to access the ManifestResourceStream returns Nothing, and the system does not raise an exception.
NOTE: If you want to verify the resource names, you can use the Microsoft Intermediate Language Disassembler (ILDASM) to view the Manifest data, which lists the included resources.
Access Resources
To access the resources that you have embedded in the Manifest of your assembly, import the System.IO and the System.Reflection namespaces, as follows:
using System.IO;
using System.Reflection;
When you declare the following in the general declaration area, the resources from the assembly are read when the form is loaded:
Assembly _assembly;
Stream _imageStream;
StreamReader _textStreamReader;
To read the resource from the assembly that is executing the current code, you must obtain an instance of that assembly. To do this, use the GetExecutingAssembly method of the assembly, as follows:
_assembly = Assembly.GetExecutingAssembly();
_imageStream = _assembly.GetManifestResourceStream("MyNameSpace.MyImage.bmp");
_textStreamReader = new StreamReader(_assembly.GetManifestResourceStream("MyNameSpace.MyTextFile.txt"));
try
{
_assembly = Assembly.GetExecutingAssembly();
_imageStream = _assembly.GetManifestResourceStream("MyNamespace.MyImage.bmp");
_textStreamReader = new StreamReader(_assembly.GetManifestResourceStream("MyNamespace.MyTextFile.txt"));
}
catch
{
MessageBox.Show("Error accessing resources!");
}
Display Resources
This example uses two buttons to display the embedded resources. When you click the first button, a bitmap image that is based on the resource that is read from the assembly is created and displayed in the PictureBoxTo display the embedded resources, follow these steps: control of the form. The second button reads from a text resource and displays the text in a text box.
- Add a PictureBox control to the form.
- Add a new Button control to the form, and then change its Text property to Show Image.
- Double-click the button to open its Click event in the code viewer, and then paste the following code in this event:This code generates a new instance of a bitmap that is based on the resource stream that was read in the Load event of the form.
try
{
pictureBox1.Image = new Bitmap(_imageStream); }
catch
{
MessageBox.Show("Error creating image!");
}
- Add a TextBox control to the form.
- Add another Button control to the form, and then change its Text property to Get Text.
- Double-click the button in the Design Editor to open the Click_Event for the button, and then paste the following code in the event:This code determines whether characters to be read still exist in the stream. If characters are found, a line is read to the text box.
try
{
if(_textStreamReader.Peek() != -1)
{
textBox1.Text = _textStreamReader.ReadLine();
}
}
catch
{
MessageBox.Show("Error writing text!");
}
- Press F5 to run the application.
No comments:
Post a Comment