Recipe 9.1. Creating and Deleting a Key


Problem

You want to create or delete a Registry key.

Solution

Using a graphical user interface

  1. Open the Registry Editor (regedit.exe).

  2. Browse to the location where you want to create or delete a key.

  3. To create a key:

    1. Right-click the parent key and select New Key.

    2. To delete a key:

      1. Right-click the key you want to delete and select Delete.

      2. Click Yes to confirm.

    Using a command-line interface

    The following command creates a Registry key in the HKLM hive called Rallencorp:

    > reg add \\<ComputerName>\HKLM\Software\Rallencorp

    The following command deletes the same Registry key:

    > reg delete \\<ComputerName>\HKLM\Software\Rallencorp

    You will be prompted to confirm the deletion. Use the /f option to delete the key and bypass the confirmation prompt.

    If you have a Registry file (i.e., a file with a .reg extension), you can also import it using regedit:

    > regedit /s <Filename>

    The /s option suppresses all windows and dialog boxes. See MS KB 310516 for more on creating Registry files.

    Using VBScript
    ' This code creates a Registry key. ' ------ SCRIPT CONFIGURATION ------ const HKLM = &H80000002 strKeyPath = "<RegKey>"      ' e.g. Software\Rallencorp strComputer = "<ComputerName>" ' e.g. wks01 (use "." for local computer) ' ------ END CONFIGURATION --------- set objReg = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv") intRC = objReg.CreateKey(HKLM, strKeyPath) if intRC <> 0 then    WScript.Echo "Error creating key: " & intRC else    WScript.Echo "Successfully created key " & strKeyPath end if ' This code deletes a Registry key. ' ------ SCRIPT CONFIGURATION ------ const HKLM = &H80000002 strKeyPath = "<RegKey>"      ' e.g. Software\Rallencorp strComputer = "<ComputerName>" ' e.g. wks01 (use "." for local computer) ' ------ END CONFIGURATION --------- set objReg = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv") intRC = objReg.DeleteKey(HKLM, strKeyPath) if intRC <> 0 then    WScript.Echo "Error deleting key: " & intRC else    WScript.Echo "Successfully deleted key " & strKeyPath end if

    Discussion

    A Registry key is nothing more than a container of other keys and Registry values. There are six root keys that are used to logically group similar Registry data. Table 9-2 contains a list of each of the root keys and describes their purpose.

    Table 9-2. The six root keys of the Registry

    Root Key

    Description

    HKEY_LOCAL_MACHINE

    This is the most important root key. It is where most system and application configuration data is stored. It is abbreviated HKLM.

    HKEY_CURRENT_USER

    This is actually a link to the subkey under HKEY_USERS for the currently logged on user. This is useful because it allows applications to access a single Registry path to get any configuration information for the currently logged on user. It is abbreviated HKCU.

    HKEY_CURRENT_CONFIG

    This is also a link that points under HKLM to hardware information for the current hardware profile. Since you can have different hardware profiles (as you can user profiles), this allows applications to access the one that is currently in use. It is abbreviated HKCC.

    HKEY_CLASSES_ROOT

    The subkeys under this key map file extensions to the applications that own them. It is abbreviated HKCR and is a link to HKLM\Software\Classes.

    HKEY_PERFORMANCE_DATA

    This key is used by applications that want to access performance data. It doesn't actually store the performance data, but serves as an interface to the data. It is abbreviated HKPD.

    HKEY_USERS

    This key stores profile information for all users of the system. It contains things such as environment variable values and user-specific customization settings. It is abbreviated HKU.


    See Also

    Recipe 9.2 for setting a value; MS KB 310516, "How to back up, edit, and restore the Registry in Windows XP and Windows Server 2003," MS KB 82821, "Registration Info Editor (REGEDIT) Command-Line Switches," and MS KB 310516, "How To Add, Modify, or Delete Registry Subkeys and Values by Using a Registration Entries (.reg) File"



Windows XP Cookbook
Windows XP Cookbook (Cookbooks)
ISBN: 0596007256
EAN: 2147483647
Year: 2006
Pages: 408

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