Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Registry Deletekey does nothing

Status
Not open for further replies.

Dooda

IS-IT--Management
May 14, 2001
76
GB
Does anyone know if there is a probem with Registry Deletekey in D4? Writing and reading are fine but neither the example in the help pages nor the following will do anything other than tell you it's failed.
Code:
procedure TfmMain.DeleteRegistryEntry(Subkey: string);
var
  Reg: TRegistry;
begin
  Reg := TRegistry.Create;
  try
    with Reg do
    begin
      RootKey := HKEY_CURRENT_USER;
      if OpenKey('Sofware\Mine\Test', false) then
        if ValueExists(Subkey) then
          if not Reg.DeleteKey(Subkey) then
            ShowMessage(Subkey+' not deleted');
    end;
  finally
    Reg.Free;
  end;
end;
 
Try this:
Code:
      if OpenKey('Sofware\Mine\Test', false) then
        if ValueExists(Subkey) then
          if not Reg.DeleteKey('Sofware\Mine\Test\' + Subkey) then
            ShowMessage(Subkey+' not deleted');

-D
 

If ValueExists returns true you are dealing with a key value, not with a key. Use DeleteValue instead of DeleteKey.

buho (A).
 
Many thanks for the suggestions, guys, but neither seems to make any difference! Has Delphi 4 EVER deleted a registry key?
 
TRegister methods are basically shells for the register Win API. I doubt D4 having a bug there.

Please, realize a value is not a key and both can't be mixed. If your "subkey" param is actually part of a key path you need to use DeleteKey. OTOW, if it is a value you need to use DeleteValue.

Lets say your key tree is like this:

- HKCU
-- Software
--- Mine
---- Test
----- Value1 = 1
----- Value2 = 2

Assigning the root HKCU and using
DeleteKey('Software\Mine\Test')
will delete the "Test" branch and all values and all possible sub-branchs and values.

Assigning the root HKCU, opening 'Software\Mine\Test' and using
DeleteValue('Value1')
will delete only the value "Value1".

Mixing both as in
DeleteKey('Software\Mine\Test\Value1')
or in
DeleteValue('Software\Mine\Test\Value1')
will not work.

buho (A).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top