Setting Values with REG Files

You learned how to create REG files using Regedit in Chapter 2, "Using the 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 you learn about in this chapter. Their big weakness is that you can't remove values using a REG file; 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 is great if you want users to import the file themselves, but you need the following command if you want to import a REG file using your software management infrastructure or some method such as providing a link to it on the intranet: regedit /s filename.reg. 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 writes REG_EXPAND_SZ and REG_MULTI_SZ strings to REG files using ANSI character encoding, so each character is a single byte. Unicode character encoding uses two bytes for each character, and when you create a Unicode REG file, Regedit writes REG_EXPAND_SZ and REG_MULTI_SZ strings to the file using the two-byte Unicode encoding scheme. 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 the registry data contains localized text that requires Unicode to represent it. If in doubt, always create version 5, Unicode files.

Listing 9-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 a version 5, Unicode REG file. The header REGEDIT4 indicates 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. They use the full names of root keys, not the abbreviations. Listing 9-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 in to the registry. The format is "name"=value. The value named @ represents the key's default value. Some of the values in Listing 9-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 talk about those a bit later.

Listing 9-5: Example.reg

start example

 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" 

end example

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:

  1. Click the key at the top of the branch you want to export.

  2. On the File menu, click Export.

    The Export Registry File dialog box appears, shown in Figure 9-2 on the next page.

    click to expand
    Figure 9-2: The only two types of files that create REG files are Registration Files (*.reg) and Win9x/NT4 Registration Files (*.reg).

  3. In the File Name box, enter a name for the file you're creating.

  4. Select the option for the export range you want:

    • To back up the entire registry, select the All option.

    • To back up the selected branch, select the Selected Branch option.

  5. In the Save As Type list, click the type of file you want to create: Registration (*.reg) or Win9x/NT4 Registration (*.reg).

  6. Click Save.

The REG file you create contains all the subkeys and values under the key you exported. The likelihood that you want all the key's subkeys and values isn't very high, so you should open the file in Notepad by right-clicking it and clicking Edit; 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 you 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, 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 don't like.

Creating REG Files Manually

Creating REG files by hand is an error-prone process that I don't recommend. Nonetheless, many of you are likely to 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:

  1. Create a new file in Notepad.

  2. At the top of the file, add one of the following, followed by a blank line:

    • Add REGEDIT4 at the top of the file to create a version 4 REG file.

    • Add Windows Registry Editor Version 5.00 at the top of the file to create a version 5 REG file.

  3. 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.

  4. 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 9-2 for information about how to format the different types of values in a REG file. You can continue an entry from one line to the next using the line-continuation character, a backslash (\).

    Table 9-2: Value Formats in REG files

    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

  5. Click File, Save As, type the name of the file in File Name, including the extension .reg (enclose the file name in quotation marks so that Notepad doesn't use the .txt extension), do one of the following, and then click Save:

    • In the Encoding list, choose ANSI to create a version 4 REG file.

    • In the Encoding list, choose Unicode to create a version 5 REG file.

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 an ages-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 9-3 describes the special characters you can use and shows you examples.

Table 9-3: Special Characters in REG Files

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 in to the registry:

 Windows Registry Editor Version 5.00 [-HKEY_CURRENT_USER\Software\Honeycutt] 

Rather than manually create 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.



Microsoft Windows XP Registry Guide
Microsoft Windows XP Registry Guide (Bpg-Other)
ISBN: 0735617880
EAN: 2147483647
Year: 2005
Pages: 185

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