Chapter 2, illustrates that the registry is one of Windows XP’s most crucial data structures. However, the registry isn’t a tool wielded only by Windows XP. Most 32-bit applications make use of the registry as a place to store setup options, customization values selected by the user, and much more. Interestingly, your scripts can get in on the act as well. Not only can your scripts read the current value of any registry setting, but they can also use the registry as a storage area. This lets you keep track of user settings, recently used files, and any other configuration data that you’d like to save between sessions. This section shows you how to use the WshShell object to manipulate the registry from within your scripts.
To read any value from the registry, use WshShell’s RegRead method:
RegRead(strName)
| The name of the registry value or key that you want to read. |
If strName ends with a backslash (), RegRead returns the default value for the key; otherwise, RegRead returns the data stored in the value. Note, too, that strName must begin with one of the following root key names:
Short Name | Long Name |
---|---|
HKCR | HKEY_CLASSES_ROOT |
HKCU | HKEY_CURRENT_USER |
HKLM | HKEY_LOCAL_MACHINE |
N/A | HKEY_USERS |
N/A | HKEY_CURRENT_CONFIG |
The following example displays the name of the registered owner of this copy of Windows XP:
Set objWshShell = WScript.CreateObject("WScript.Shell") strSetting = "HKLMSOFTWAREMicrosoft" & _ "Windows NTCurrentVersionRegisteredOwner" strRegisteredUser = objWshShell.RegRead(strSetting) WScript.Echo strRegisteredUser
To store a setting in the registry, use WshShell’s RegWrite method:
RegWrite strName, anyValue [, strType]
| The name of the registry value or key that you want to set. If strName ends with a backslash (), RegWrite sets the default value for the key; otherwise, RegWrite sets the data for the value. strName must begin with one of the root key names detailed in the RegRead method. |
| The value to be stored. |
| The data type of the value, which must be one of the following: REG_SZ (the default), REG_EXPAND_SZ, REG_DWORD, or REG_BINARY. |
The following statements create a new key named ScriptSettings in the HKEY_CURRENT_USER root:
Set objWshShell = WScript.CreateObject("WScript.Shell") objWshShell.RegWrite "HKCUScriptSettings", ""
The following statements create a new value named NumberOfReboots in the HKCUScriptSettings key, and set this value to 1:
Set objWshShell = WScript.CreateObject("WScript.Shell") objWshShell.RegWrite "HKCUScriptSettingsNumberOfReboots", 1, _ "REG_DWORD"
If you no longer need to track a particular key or value setting, use the RegDelete method to remove the setting from the registry:
RegDelete (strName)
| The name of the registry value or key that you want to delete. If strName ends with a backslash (), RegDelete deletes the key; otherwise, RegDelete deletes the value. strName must begin with one of the root key names detailed in the RegRead method. |
To delete the NumberOfReboots value used in the previous example, you would use the following statements:
Set objWshShell = WScript.CreateObject("WScript.Shell") objWshShell.RegDelete "HKCUScriptSettingsNumberOfReboots"
18.116.15.161