Resource files are the key to language localization in .NET programs. Visual Studio will write the files for you, but it's good to know something about how they work, because you may want to craft your own resource files (if you have a lot of time on your hands). The life of a resource moves through three phases, as determined by the type of file in which it appears.
Source. An application's resources start out their lives in a resource source file. Before .NET, resources appeared in "resource script" files, which merged all the best of C-language development and UPPER CASE SCRIPT COMMANDS, and used an ".rc" file extension. In Visual Basic 2005, you use XML-based ".resx" files. Every new Windows Forms application already includes a Resources.resx file just waiting to be joyfully filled with your application resources.
Beyond the core resource source files, other file types can be included as resources, although they are still referenced through the .resx file content. Common external resource files include image files (such as .gif and .jpg files) and plain text files (.txt). The Library Project uses a file named SplashImage.jpg as a resource for the splash screen, and another file named ItemLookupBody.txt that contains HTML content used when displaying items through the ItemLookup.vb form.
Intermediate. Once you have your resource sources ready, they are converted into an intermediate form, and stored with a ".resources" file extension, through a process called resource generation. Visual Studio normally does this step behind the scenes for you, but you can also use a tool supplied with the .NET SDK (called resgen.exe) to generate these files yourself. Intermediate resource files include binary content only, and are not designed for browsing in Notepad.
Compiled. Intermediate resource files aren't much use to your deployed application. The term "intermediate" kind of gave this secret away, didn't it? Before employing the resources in your program, they need to be compiled into a DLL or EXE file. Perhaps you already knew that these files contained multiple sections, including distinct code and data sections. A compiled resource file contains only a data section with the resources; there is no code in a compiled resource file, although standard compiled code files may also include compiled resources.
In .NET, compiled resource files are satellite assemblies. They support your primary application assembly, and are not generally useful apart from that master assembly.
Figure 18-1 shows the lifetime of a resource through these three stages.
Figure 18-1. The edible life of a resource
There are some standard resource types stored in .NET resource (.resx) files.
Strings. We'll focus primarily on string resources in this chapter. Each string resource includes a name and a string value.
Images. Visual Basic applications can include JPEG, GIF, TIFF, PNG, and BMP image files. Each image, as with all resources, includes an associated name, which may differ from the original name of the graphic file.
Icons. Program icons used with forms and the application itself appear as standard resources. Icons have an ".ico" file extension.
Audio. Resources can include named audio files, based on WAV audio content.
Files. If the file types listed so far don't meet your needs, you can include whole files of any type as a named resource.
Other. Beyond files, you can store the content of any valid .NET data type as a resource. The resources in a .resx file are actually strongly typed to .NET types, so there's really no limit to the type of data you can place there. You can also modify the underlying .resx file to include "non-standard" resources. Non-standard resources are beyond the scope of this chapter.
The project properties window includes a manager for application-wide resources (see Figure 18-2). The IDE also includes special editors that let you edit standard and a few non-standard resources types.
Figure 18-2. The resource manager for the Library Project in Visual Studio