Hives have an arcane format, making direct editing all but futile. Fortunately, the Registry Editor conveniently supports the import and export of any number of keys and values with Registry patches. Patches (.reg files) are ordinary text files that can contain anything from a single key to a dump of the entire Registry.
You can create Registry patches with the Registry Editor or a standard text editor, such as Notepad. You can also use Notepad to view and modify patches, and then use the Registry Editor to reimport the patch.
Patches have many practical uses, including creating local backups of portions of the Registry as a preventive measure before editing keys (see the preceding section). You can create a Registry key on one computer and apply it on another, which is useful for migrating a single setting or a whole group of settings to any number of Windows systems. Patches can allow easier editing than with the Registry Editor, and they certainly afford quicker and more flexible searches.
To create a Registry patch, highlight the key you want to export and select File Export. Once you've chosen a filename, the selected key, any subkeys, and all their values and respective data will be saved in a single file with the .reg extension. In most cases, you wouldn't want to select My Computer to export the entire Registry, because HKLM is enormous and you wouldn't want to reimport it in any case.
Importing .reg files isn't quite as simple as creating them, partly because of the concept of merging and partly because of the potential for harm. It's important to note that the contents of a Registry patch are merged with existing keys; they don't simply overwrite them. So, if a given key contains four keys (apple, pear, banana, and peach), and you apply a Registry patch (pointing to the same key) that contains four keys (apple, pear, banana, and pomegranate), the resulting key will have five keys (apple, pear, banana, peach, and pomegranate). The existing values and keys will indeed be overwritten with those in the patch, but any additional values and keys in the Registry will remain intact.
The format of .reg files is similar to that of .ini files, rather than anything resembling the way data is displayed in the Registry Editor. A section begins with the full path of a key in square brackets, like this: [section name]. This is followed by any number of values, each of the format name="data" (the default value appears as @="value"). Then, the next key (if any) is listed, followed by all the values it contains. The order of the keys, as well as the order of the values in each key, is irrelevant. However, if you were to move a key from one section to another, it would, in effect, be moving the value from one key to another.
The quotes around value data are used only for string values; binary and REG_EXPAND_SZ values are prefixed with the keyword hex: and appear without quotes. Similarly, DWORD values are preceded by dword: and appear without quotes. Any backslashes in value data (found most often in folder and Registry paths) are doubled to distinguish them from the backslashes in key names.
Lastly, the first line in every Registry patch created in Windows Vista will be Windows Registry Editor Version 5.00, followed by a blank line. (The same holds true for Windows XP and Windows 2000.) Patches created in Windows 95, 98, and Me will instead begin with Registry Editor4, also followed by a blank line. There doesn't appear to be any difference in the treatment of these two types of patches by the Registry Editor.
Here is an excerpted Registry patch:
Windows Registry Editor Version 5.00 [HKEY_CURRENT_USER\Software\mozilla.org\Mozilla\0.9.2 (en)\Main] "Program Folder Path"="C:\\Documents and Settings\\All Users\\ Start Menu\\Programs\\Mozilla\\" "Install Directory"="C:\\Program Files\\Mozilla\\" [HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main] "NoUpdateCheck"=dword:00000001 "Show_ChannelBand"="No" "Display Inline Images"="yes" "Show_ToolBar"="yes" "Check_Associations"="no" "SmoothScroll"=dword:00000001 "Play_Animations"="yes" "Play_Background_Sounds"="yes" "Display Inline Videos"="yes" "Print_Background"="no"
You may notice that these two keys are in different manufacturer branches and wouldn't appear next to each other in the Registry. If you think about the way Registry patches are created, you'll realize that the one shown here couldn't have been created in a single step. Instead, two different Registry patches were created, and the contents of one were cut and pasted into the other using a plain-text editor. As long as you remove the redundant header, this is perfectly acceptable. In fact, it can be a very convenient way to implement several Registry changes in one step.
Extra-credit question: given the structure of file types discussed earlier in this chapter, how would you create a single Registry patch that contains all the necessary information to register a new file association on any computer? Could you use such a patch to restore your preferred file type settings if another application were to ever overwrite them?