Previous Topic: RegCreateKey - Create a Registry keyNext Topic: RegDeleteValue - Delete a Value in the Registry


RegDeleteKey - Delete a Registry Key

Valid on Windows

The RegDeleteKey function removes the specified key.

This registry manipulation function has the format:

RegDeleteKey(hKey as Integer, subkey as String) as Boolean
hKey

Indicates a currently open key or a predefined key and can be any of the following predefined values, which are available on all Windows platforms:

HKEY_CLASSES_ROOT
HKEY_CURRENT_USER
HKEY_LOCAL_MACHINE
HKEY_USERS
subkey

Indicates the parameter is a string that specifies the name of the key to delete. Subkey must be a subkey of the key identified by hKey. Do not use an empty string as a subkey parameter.

On successful completion, the function returns TRUE. If the key does not exist or is currently opened by another application, it returns FALSE.

subkeys

On Windows 9x, this function also removes all subkeys. On Windows NT and 2000, you must remove each subkey separately before running the function.

Example: RegDeleteKey function

Test structure:

HKLM\

\ Software

\ CA

\ 4 test only

\ var_6

\ dummy

Dim hkey1 as integer

ClrScr()


hkey1 = RegOpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE\CA\4 test only")
if hkey1 = 0 then
	Print("RegOpenKey failed.")
	exit
endif

if RegDeleteValue(hKey1) then
	Print("RegDeleteValue succeeded.")
else
	Print("RegDeleteValue failed.")
endif

if RegDeleteVariable(hKey1, "var_6") then
	Print("RegDeleteVariable for var_6 succeeded.")
else
	Print("RegDeleteVariable for var_6 failed.")
endif

RegCloseKey(hkey1)

hkey1 = RegOpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE\CA")

if hkey1 = 0 then
	Print("RegOpenKey failed.")
	exit
endif

if RegDeleteKey(hkey1, "dummy") then
	Print("Key dummy successfully deleted")
else
	Print("Key dummy deletion failed.")
endif

RegCloseKey(hkey1)

Messagebox("Check registry!", "desktop management scripting test")

hkey1 = RegOpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE")
if hkey1 = 0 then
	Print("RegOpenKey failed.")
	exit
endif

if RegDeleteKey(hkey1, "CA\4 test only") then
	Print("Key ""CA\4 test only"" successfully deleted")

else
	Print("Key" "CA\4 test only" "deletion failed.")
endif

if RegDeleteKey(hkey1, "CA") then
	Print("Key CA successfully deleted")
else
	Print("Key CA deletion failed.")
endif

RegCloseKey(hkey1)