Setting Values with REG Files
You learned how to use Regedit to create REG files in Chapter 2, “Using Registry Editor.” REG files are the classic method for adding and changing values in the registry, but as I said in the section “Choosing a Technique,” they're not as powerful as the other methods that you learn about in this chapter. The big weakness of REG files is that you can't use them to remove values; you can only add or modify values, or remove keys.
After you've created a REG file, which has the .reg file extension, you import it into the registry by double-clicking the file. This method is great if you want users to manually import the file, but you need the command regedit /s filename.reg if you want to import a REG file by using your software management infrastructure or by providing a link to it on the intranet. Replace filename.reg with the path and name of your REG file. The /s command-line option imports the file into the registry without prompting the user, which is what you want to do most of the time. To edit a REG file, right-click it, and then click Edit. Don't accidentally double-click a REG file thinking that you're going to open it in Notepad, because double-clicking a REG file imports it into the registry.
Remember that Regedit supports two different file formats for REG files. Version 4 REG files are ANSI. ANSI character encoding uses one byte to represent each character. Also, Regedit uses ANSI character encoding to write REG_EXPAND_SZ and REG_MULTI_SZ strings to REG files, so each character is a single byte. Version 5 REG files are Unicode. Unicode character encoding uses two bytes for each character, and when you create a Unicode REG file, Regedit uses the two-byte Unicode encoding scheme to write REG_EXPAND_SZ and REG_MULTI_SZ strings to the file. Chapter 1, “Learning the Basics,” tells you more about the differences between the two encoding standards. Chapter 2, “Using Registry Editor,” describes the differences between the two different types of REG files. What you need to know is that choosing to create a version 4 REG file means that the file and the values in the file use ANSI; likewise, creating a version 5 REG file means that the file and the values in the file use Unicode. I tend to use version 4, ANSI REG files, except when I know that the registry data contains localized text that requires Unicode to represent it. If in doubt, always create version 5, Unicode files.
Listing 11-5 shows a sample REG file. The first line in this file is the header, which identifies the file's version. The header Windows Registry Editor Version 5.00 indicates that this is a version 5, Unicode REG file. The header REGEDIT4 would indicate a version 4, ANSI REG file. A blank line usually follows the header, but the file works fine without it. Notice how similar the remainder of this file looks to INF and INI files. Each section contains the fully qualified name of a key. Each section also uses the full names of root keys, rather than the abbreviations for them. Listing 11-5 is importing settings into three keys: HKCU\Control Panel\Desktop, HKCU\Control Panel\Desktop\WindowMetrics, and HKCU\Control Panel\Mouse. The lines below each section are values that Regedit will add to that key when Regedit imports the file into the registry. The format is “name"=value. The value named @ represents the key's default value. Some of the values in Listing 11-5 contain dword and hex, whereas others are enclosed in quotation marks. Values enclosed in quotation marks are strings. Values in the form dword:value are REG_DWORD values. Values in the form hex:values are REG_BINARY values. This gets more complicated when you add subtypes, such as hex(type):value, and I'll explain those later.
Listing 11-5 Example.reg
Windows Registry Editor Version 5.00 [HKEY_CURRENT_USER\Control Panel\Desktop] "ActiveWndTrkTimeout"=dword:00000000 "ForegroundFlashCount"=dword:00000003 "ForegroundLockTimeout"=dword:00030d40 "MenuShowDelay"="400" "PaintDesktopVersion"=dword:00000000 "UserPreferencesMask"=hex:9e,3e,07,80 [HKEY_CURRENT_USER\Control Panel\Desktop\WindowMetrics] "Shell Icon BPP"="16" "Shell Icon Size"="32" "MinAnimate"="1" [HKEY_CURRENT_USER\Control Panel\Mouse] @="Rodent" "ActiveWindowTracking"=dword:00000000 "DoubleClickHeight"="4" "DoubleClickSpeed"="500" "DoubleClickWidth"="4" "MouseSensitivity"="10" "MouseSpeed"="1" "MouseThreshold1"="6" "MouseThreshold2"="10" "SnapToDefaultButton"="0" "SwapMouseButtons"="0"
Exporting Settings to REG Files
The easiest way to create a REG file is by using Regedit to export keys to REG files. Follow these steps to export branches of the registry to files:
Click the key at the top of the branch that you want to export.
On the File menu, click Export.
The Export Registry File dialog box appears, shown in Figure 11-2.
In the File Name box, enter a name for the file you're creating.
Select the option for the export range that you want:
To back up the entire registry, select the All option.
To back up the selected branch, select the Selected Branch option.
In the Save As Type drop-down list, click the type of file that you want to create: Registration (*.reg) or Win9x/NT4 Registration (*.reg).
Click Save.
Figure 11-2 The only two types of files that create REG files are Registration files (*.reg) and Win9x/NT4 Registration files (*.reg).
The REG file that you create contains all the subkeys and values under the key that you exported. It's not likely that you want all the key's subkeys and values, so right-click the file and click Edit to open it in Notepad, and then remove any keys and values that you don't want to keep in the file. You can also change any of the values in the REG file. For example, you can export a key from your own computer, just to get started, and then edit it to suit your requirements, removing keys, changing values, and so on.
CAUTION
If you're creating a REG file for versions of Windows that don't support version 5, Unicode REG files, then use version 4, ANSI REG files. Microsoft Windows 95, Windows 98, and Windows Me do not support Unicode REG files, and any attempt to import Unicode REG files into their registries could yield results that you won't like.
Creating REG Files Manually
Creating REG files by hand is an error-prone process that I don't recommend. Nonetheless, many people do it anyway, so I'm going to show you how. First decide whether you're going to create an ANSI or Unicode REG file, and then follow these instructions to create it:
Create a new file in Notepad.
At the top of the file, add one of the following, followed by a blank line:
To create a version 4 REG file, add REGEDIT4.
To create a version 5 REG file, add Windows Registry Editor Version 5.00.
For each key into which you want to import values, add a section to the file in the format [key], where key is the fully qualified name of the key. Don't use the root-key abbreviations; use their full names: HKEY_CURRENT_USER.
For each value that you want to import into the registry, add the value in the format "name"=value to the key's section. Use @ for a key's default value. See Table 11-2 for information about how to format the different types of values in a REG file. You can use the line-continuation character, a backslash (\), to continue an entry from one line to the next.
Click File, Save As, and type the name of the file in File Name box, including the extension .reg. (Enclose the file name in quotation marks so that Notepad doesn't use the .txt extension). Then, from the Encoding drop-down list, choose one of the following, and then click Save:
To create a version 4 REG file, choose ANSI.
To create a version 5 REG file, choose Unicode.
Type | Version 4 | Version 5 |
REG_SZ | "String" | "String" |
REG_DWORD | dword:00007734 | dword:00007734 |
REG_BINARY | hex:00,00,01,03 | hex:00,00,01,03 |
REG_EXPAND_SZ | hex(2):25,53,59,53,54,45,4d,52,4f,4f,54,25,00 | hex(2):25,00,53,00,59,00,53,00, 54,00,45,00,4d,00,52,00,4f,00, 4f,00,54,00,25,00,00,00 |
REG_MULTI_SZ | hex(7):48,65,6c,6c,6f,20,57,6f,72,6c,64,00,4a,65,72,72,79,20, 77,61,73,20,68,65,72,65,00,00 | hex(7):48,00,65,00,6c,00,6c,00, 6f,00,20,00,57,00,6f,00,72,00, 6c,00,64,00,00,00,4a,00,65,00, 72,00,72,00,79,00,20,00,77,00, 61,00,73,00,20,00,68,00,65,00, 72,00,65,00,00,00,00,00 |
Encoding Special Characters
Within REG files, certain characters have special meaning. Quotation marks begin and end strings. The backslash character is a line-continuation character. So how do you include these characters in your values? You use escaping, which is a very old method for prefixing special characters with a backslash. For example, the string \n represents a newline character, and the string \" represents a quotation mark. Table 11-3 describes the special characters that you can use and shows you examples.
Escape | Expanded | Example |
\\ | \ | C:\\Documents and Settings\\Jerry |
\" | " | A \"quoted\" string |
\n | newline | This is on \n two lines |
\r | return | This is on \r two lines |
Deleting Keys Using a REG File
You can't use a REG file to remove individual values, but you can certainly use one to delete entire keys. This is an undocumented feature of REG files: just prefix a key's name with a minus (–) sign: [-key]. Here's a brief example that removes the key HKCU\Software\Honeycutt when you import the REG file into the registry:
Windows Registry Editor Version 5.00 [-HKEY_CURRENT_USER\Software\Honeycutt]
Rather than manually creating a REG file to remove keys, I prefer to export a key to a REG file and then edit it. After exporting the key to a REG file, remove all the values and keys that you don't want to delete. Then add the minus sign to the names of the keys that you want to delete. Then you can remove those keys quickly and easily by double-clicking the REG file or using the command regedit /s filename.reg.