DelphiDabbler Code Snippets Database

Snippet Selection

The following snippets from the Code Snippets Database have been requested.

GetRegistryString

Reads and returns the value named by Name from the registry sub-key SubKey in root key RootKey. String, expanded string and integer value types are all handled, with integer values being converted to strings. Other value types are not supported and cause an exception to be raised. The empty string is returned if the named value does not exist.

function GetRegistryString(const RootKey: Windows.HKEY;
  const SubKey, Name: string): string;
var
  Reg: Registry.TRegistry;          // registry access object
  ValueInfo: Registry.TRegDataInfo; // info about registry value
begin
  Result := '';
  // Open registry at required root key
  Reg := Registry.TRegistry.Create;
  try
    Reg.RootKey := RootKey;
    // Open registry key and check value exists
    if Reg.OpenKeyReadOnly(SubKey)
      and Reg.ValueExists(Name) then
    begin
      // Check if registry value is string or integer
      Reg.GetDataInfo(Name, ValueInfo);
      case ValueInfo.RegData of
        Registry.rdString, Registry.rdExpandString:
          // string value: just return it
          Result := Reg.ReadString(Name);
        Registry.rdInteger:
          // integer value: convert to string
          Result := SysUtils.IntToStr(Reg.ReadInteger(Name));
        else
          // unsupported value: raise exception
          raise SysUtils.Exception.Create(
            'Unsupported registry type'
          );
      end;
    end;
  finally
    // Close registry
    Reg.Free;
  end;
end;

RegDeleteValue

Deletes the value specified by APath from the registry root key ARootKey, where APath is a combination of a sub-key and value name, separated by a backslash. If the value does not exist or can't be accessed then no action is taken.

procedure RegDeleteValue(const ARootKey: Windows.HKEY; const APath: string);
var
  SubKey: string;     // registry sub-key extracted from APath
  ValueName: string;  // value name extracted from APath
begin
  with Registry.TRegistry.Create do
    try
      RootKey := ARootKey;
      RegParsePath(APath, SubKey, ValueName);
      if OpenKey(SubKey, True) then
        DeleteValue(ValueName);
    finally
      Free;
    end;
end;

RegKeyList

Retrieves a list of keys contained in registry sub-key ASubKey within root key ARootKey. The key names are stored in string list AKeyList. If there are no keys or there is an error then AKeyList is emptied.

procedure RegKeyList(const ARootKey: Windows.HKEY; const ASubKey: string;
  const AKeyList: Classes.TStrings);
begin
  with Registry.TRegistry.Create do
    try
      RootKey := ARootKey;
      if OpenKeyReadOnly(ASubKey) then
        GetKeyNames(AKeyList)
      else
        AKeyList.Clear;
    finally
      Free;
    end;
end;

RegParsePath

Parses string APath that describes a registry path into sub-key and value name components which are passed out in the ASubKey and AValueName parameters respectively. APath is considered to comprise a sub-key followed by a backslash and the value name. APath must not include the root key.

procedure RegParsePath(const APath: string; out ASubKey, AValueName: string);
begin
  ASubKey := APath;
  AValueName := '';
  while (Length(ASubKey) > 0) and (LastChar(ASubKey) <> '\') do
  begin
    AValueName := LastChar(ASubKey) + AValueName;
    StripLastChar(ASubKey);
  end;
end;

RegReadString

Reads and returns a string value from the registry. The string to be read is located under root key ARootKey in the value specified by APath, which is a combination of a sub-key and value name, separated by a backslash. If the value does not exist, or there is an error, then the value given by ADefault is returned.

function RegReadString(const ARootKey: Windows.HKEY;
  const APath, ADefault: string): string;
var
  SubKey: string;     // registry sub-key extracted from APath
  ValueName: string;  // value name extracted from APath
begin
  with Registry.TRegistry.Create do
    try
      try
        begin
          RootKey := ARootKey;
          RegParsePath(APath, SubKey, ValueName);
          if OpenKeyReadOnly(SubKey) then
          begin
            if ValueExists(ValueName) then
              Result := ReadString(ValueName)
            else
              Result := ADefault;
          end
          else
            Result := ADefault;
        end
      except
        Result := ADefault;
      end;
    finally
      Free;
    end;
end;

RegValueExists

Checks if the value specified by APath exists in the registry under root key ARootKey. Returns True if the value exists or False if not. APath is considered to be a combination of a sub-key and value name, separated by a backslash.

function RegValueExists(const ARootKey: Windows.HKEY; const APath: string):
  Boolean;
var
  SubKey: string;     // registry sub-key extracted from APath
  ValueName: string;  // value name extracted from APath
begin
  with Registry.TRegistry.Create do
    try
      RootKey := ARootKey;
      RegParsePath(APath, SubKey, ValueName);
      if OpenKeyReadOnly(SubKey) then
        Result := ValueExists(ValueName)
      else
        Result := False;
    finally
      Free;
    end;
end;

RegValueList

Retrieves a list of values names contained in registry sub-key ASubKey within root key ARootKey. The values names are stored in string list AValueList. If there are no value names, or there is an error, then AValueList is emptied.

procedure RegValueList(const ARootKey: Windows.HKEY; const ASubKey: string;
  const AValueList: Classes.TStrings);
begin
  with Registry.TRegistry.Create do
    try
      RootKey := ARootKey;
      if OpenKeyReadOnly(ASubKey) then
        GetValueNames(AValueList)
      else
        AValueList.Clear;
    finally
      Free;
    end;
end;

RegWriteString

Sets the registry value specified by APath under root key ARootKey to the given string AValue. APath is considered to be a combination of a sub-key and value name, separated by a backslash. Does nothing if the sub-key does not exist.

procedure RegWriteString(const ARootKey: Windows.HKEY;
  const APath, AValue: string);
var
  SubKey: string;     // registry sub-key extracted from APath
  ValueName: string;  // value name extracted from APath
begin
  with Registry.TRegistry.Create do
    try
      RootKey := ARootKey;
      RegParsePath(APath, SubKey, ValueName);
      if OpenKey(SubKey, True) then
        WriteString(ValueName, AValue);
    finally
      Free;
    end;
end;

View the whole database.

Go to the DelphiDabbler website.