11.11 Saving Resource Information Using the ResourceWriter Class

 <  Day Day Up  >  

11.11 Saving Resource Information Using the ResourceWriter Class

You want to create a custom resource that you can use within a satellite assembly.


Technique

Create the object definition of the resource you want to store in a satellite assembly, making sure that you place the Serializable attribute before the class declaration:

 
 using System; using System.Drawing; namespace _11_ResourceObjects {     [Serializable]     public class ImageWithCaption     {         public string caption;         public Image image;         public ImageWithCaption( string caption, Image image )         {             this.caption = caption;             this.image = image;         }         public string Caption         {             get             {                 return this.caption;             }             set             {                 this.caption = value;             }         }         public Image Picture         {             get             {                 return this.image;             }             set             {                 this.image = value;             }         }     } } 

After creating an instance of the resource object, or any other object, you can save it to a .resources file by using the ResourceWriter class or a .resx file using a ResXResourceWriter instance. Using either one of these classes is similar, with the difference being the file format output. Each constructor expects a Stream to write data to or a string value denoting the filename. After you create the appropriate object, use the AddResource method, passing an object to save to the resource file. After you add all the necessary resources, call the Generate method to create the resource file. Keep in mind that any attempts to add resources after the Generate method has been called result in an exception.

The following function uses the list view data in Figure 11.5 to write several ImageWithCaption objects, which were shown earlier:

 
 private void btnSave_Click(object sender, System.EventArgs e) {     if( saveFileDialog1.ShowDialog() == DialogResult.OK )     {         ResourceWriter rw = new ResourceWriter( saveFileDialog1.FileName );         foreach( ListViewItem lvItem in lvItems.Items )         {             _11_ResourceObjects.ImageWithCaption obj =                  new _11_ResourceObjects.ImageWithCaption(                      lvItem.SubItems[0].Text,                      Image.FromFile( lvItem.SubItems[1].Text ));              rw.AddResource( lvItem.SubItems[0].Text, obj );         }         rw.Generate();      } } 
Figure 11.5. The Image Caption Resource Editor saves custom resource objects into a resource file that is later compiled into satellite assemblies.

graphics/11fig05.gif

Comments

We admit that when we first started looking at programmatically creating resources, we thought that we would never need to use that capability. However, after creating the code that accompanies this recipe, you could say that our eyes were opened. When you get across the hurdle of thinking that resources consist only of string or image data, you might be able to think of creative ways to use custom resources. The code that accompanies this recipe is separated into three different projects. The first project is a simple class library used to define a custom resource object named ImageWithCaption . By itself, it isn't some elaborate object that will win any awards. It's just a simple class containing a string and Image object without appropriate accessor properties.

Another project created the resource files themselves using a custom resource editor that, as you guessed, creates ImageWithCaption resource files. You can see the application running in Figure 11.5. Some of the code for this application uses the ResourceWriter class to generate .resources files. These files can then be embedded into an assembly by creating satellite assemblies or can be read at runtime, which is what the third and final application does.

You do not have to use the resource classes within the .NET Framework solely for satellite assembly resource access. You are free to use .resources directly at runtime, although we don't recommend doing so. The last project associated with this recipe reads the .resources file created by the resource editor and displays an image with its associated caption, as shown in Figure 11.6.

Figure 11.6. This application reads ImageWithCaption resource objects created with the editor in Figure 11.5.

graphics/11fig06.gif

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net