40.5. Coding Resource Files

In addition to the rich visual tools that Visual Studio 2008 now provides for editing resource files, it is possible to use code to create resource files. The .NET Framework provides support for reading and writing resource files using two interfaces: IResourceReader and IResourceWriter. Once the resource files have been created, they need to be added to the application or manually linked so that they can be referenced within the application.

  • IResource Reader: The reader interface ensures that resource readers have the following methods:

    • GetEnumerator: The GetEnumerator method retrieves an IDictionaryEnumerator object that permits the developer to iterate over each of the resources in the resource file.

    • Close: The Close method is used to close the resource reader and release any associated resources.

  • IResource Writer: The writer interface ensures that resource writers have the following methods:

    • AddResource: There are three overloads to the AddResource method that support adding resources to the resource file. Both of the framework implementations of this interface have either an additional overload of this method or an alternative method for adding resources. The overloads that are part of this interface support adding resources in a name-value pair. Each method has the resource name as the first parameter and a value, such as a string, byte array, or object, as the second parameter. The final implementation that takes an object as a parameter may need to be serializable or converted to a string via a typeconverter.

    • Close: The Close method writes resources out to the stream before closing it.

    • Generate: Unlike the Close method, the Generate method simply writes the resources out to the stream without closing it. Once this method is called, any other method will cause an exception to be raised.

40.5.1. ResourceReader and ResourceWriter

ResourceReader and ResourceWriter are an implementation of the IResource interfaces to support reading and writing directly to resources files. Although reading and writing to this format is the most direct approach, as it reduces the need to use Resgen to generate the resources file, it does limit the quality of information that can be retrieved in reading from the file. Each resource is treated as a series of bytes where the type is unknown.

40.5.2. ResxResourceReader and ResxResourceWriter

ResxResourceReader and ResxResourceWriter are more versatile implementations of the IResource interfaces. In addition to supporting the IResource interface, ResxResourceWriter supports an additional overload of the AddResource method, whereby a ResxDataNode can be added. A ResxDataNode is very similar to a dictionary entry, as it has a key (in this case, the Name property) and a value (which you must set when the node is created). However, the difference is that this node can support additional properties such as a comment and, as an alternative to a value, a file reference (for example, one that indicates where an image needs to be added to a resource file).

As mentioned previously, it is possible to add a file reference to a resx file so that the file is still editable yet has the benefit of being compiled into the resource file by resgen.exe. The supporting class in the framework is ResxFileRef. This can be instantiated and added as a resource via the ResxResourceWriter. This inserts an XML node similar to the following snippet:

<data name="Figure_11_2" type="ResXFileRef, System.Windows.Forms">

    <value>..ResourcesCompanyLogo.tif;System.Drawing.Bitmap, System.Drawing,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>

Resource Files: A Word of Caution

Resource files are the best means of storing static application data. Although they are linked in to the application as part of the compilation process, their contents can easily be extracted and made human-readable. Because of this, however, resource files are not suitable for storing secure data such as passwords and credit card information.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.42.176