RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TRegistry Class

TRegistry is a low-level wrapper for the system registry and functions that operate on the registry.

Pascal
TRegistry = class(TObject);
C++
class TRegistry : public TObject;

Use TRegistry to encapsulate access to the Windows system registry in an application. The registry is a database that an application can use to store and retrieve configuration information. Configuration information is stored in a hierarchical tree. Each node in the tree is called a key. Every key can contain subkeys and data values that represent part of the configuration information for an application. 

All keys that an application creates, opens, reads, or writes are subkeys of predefined root keys. By default, a TRegistry object is created with a root key of HKEY_CURRENT_USER. 

Only one key is accessible at a time in a TRegistry object. To determine the key that is currently accessible, read the value of the CurrentKey property. TRegistry methods enable an application to open, close, save, move, copy, and delete keys. 

One or more data values containing actual configuration information can be stored in a key. TRegistry methods enable an application to query a key to see if it contains data, to read data in a key, and to write data to a key.

Note: The TRegistry component is not fully compatable with the Windows NT environment. Functions such as RestoreKey and SaveKey may not perform properly without a significant number of workarounds.
 

C++ Examples: 

 

/*
This example shows how to use the TRegistry class in order to
find, insert and delete Keys and Items into the Windows Registry.
This example uses two buttons: InsertToRegBtn and DeleteFromRegBtn,
for inserting and deleting the values.
*/
void __fastcall TForm2::InsertToRegBtnClick(TObject *Sender)
{
    TRegistry* reg = new TRegistry(KEY_READ);
    reg->RootKey = HKEY_LOCAL_MACHINE;

    if(!reg->KeyExists("Software\\MyCompanyName\\MyApplication\\"))
    {
        MessageDlg("Key not found! Created now.",
                    mtInformation, TMsgDlgButtons() << mbOK, 0);
    }
    reg->Access = KEY_WRITE;
    bool openResult = reg->OpenKey("Software\\MyCompanyName\\MyApplication\\", true);

    if(!openResult)
    {
        MessageDlg("Unable to create key! Exiting.",
                    mtError, TMsgDlgButtons() << mbOK, 0);
        return;
    }
    //checking if the values exist and inserting when neccesary

    if(!reg->KeyExists("Creation\ Date"))
    {
        TDateTime today = TDateTime::CurrentDateTime();
        reg->WriteDateTime("Creation\ Date", today);
    }

    if(!reg->KeyExists("Licenced\ To"))
    {
        reg->WriteString("Licenced\ To", "MySurname\ MyFirstName");
    }

    if(!reg->KeyExists("App\ Location"))
    {
        reg->WriteExpandString("App\ Location",
                                "%PROGRAMFILES%\\MyCompanyName\\MyApplication\\");
    }

    if(!reg->KeyExists("Projects\ Location"))
    {
        reg->WriteExpandString("Projects\ Location",
                                "%USERPROFILE%\\MyApplication\\Projects\\");
    }

    reg->CloseKey();
}
//---------------------------------------------------------------------------
void __fastcall TForm2::DelFromRegBtnClick(TObject *Sender)
{
    //deleting the example registry entries
    TRegistry* reg = new TRegistry(KEY_WRITE);
    reg->RootKey = HKEY_LOCAL_MACHINE;

    reg->DeleteKey("Software\\MyCompanyName\\MyApplication");
    reg->DeleteKey("Software\\MyCompanyName");

    reg->CloseKey();
}
//---------------------------------------------------------------------------

 

Delphi Examples: 

{
This example shows how to use the TRegistry class in order to
find, insert and delete Keys and Items into the Windows Registry.
This example uses two buttons: InsertToRegBtn and DeleteFromRegBtn,
on a form, for inserting and deleting the values.
}
procedure TForm3.InsertToRegBtnClick(Sender: TObject);
var
  reg        : TRegistry;
  openResult : Boolean;
  today      : TDateTime;
begin
  reg := TRegistry.Create(KEY_READ);
  reg.RootKey := HKEY_LOCAL_MACHINE;

  if (not reg.KeyExists('Software\\MyCompanyName\\MyApplication\\')) then
    begin
      MessageDlg('Key not found! Created now.',
                            mtInformation, mbOKCancel, 0);
    end;
  reg.Access := KEY_WRITE;
  openResult := reg.OpenKey('Software\\MyCompanyName\\MyApplication\\',True);

  if not openResult = True then
    begin
      MessageDlg('Unable to create key! Exiting.',
                  mtError, mbOKCancel, 0);
      Exit();
    end;

  {checking if the values exist and inserting when neccesary}

  if not reg.KeyExists('Creation\ Date') then
    begin
      today := Now;
        reg.WriteDateTime('Creation\ Date', today);
    end;

  if not reg.KeyExists('Licenced\ To') then
    begin
        reg.WriteString('Licenced\ To', 'MySurname\ MyFirstName');
    end;

  if not reg.KeyExists('App\ Location') then
    begin
        reg.WriteExpandString('App\ Location',
                            '%PROGRAMFILES%\\MyCompanyName\\MyApplication\\');
    end;

  if not reg.KeyExists('Projects\ Location') then
    begin
        reg.WriteExpandString('Projects\ Location',
                            '%USERPROFILE%\\MyApplication\\Projects\\');
    end;

  reg.CloseKey();

end;

procedure TForm3.DeleteFromRegBtnClick(Sender: TObject);
var
  reg : TRegistry;
begin
  reg := TRegistry.Create(KEY_WRITE);
  reg.RootKey := HKEY_LOCAL_MACHINE;

  reg.DeleteKey('Software\\MyCompanyName\\MyApplication');
  reg.DeleteKey('Software\\MyCompanyName');

  reg.CloseKey();
end;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!