Reading Sections and Options from the System Registry. Example

Applies to TestComplete 15.45, last modified on December 01, 2022

The following script lists names and locations of all system and special folders, such as “My Documents”, “Desktop” and others. The routine reads a system registry key (a section) that keeps information about these folders via the Storages object. Using the resulting Section object, the routine iterates through all options in this section and posts their names and values to the test log.

JavaScript, JScript

function ListSystemFolders()
{
  var key, valueName, i;

  key = Storages.Registry("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", HKEY_CURRENT_USER);

  Log.AppendFolder("List of system and special folders:", key.GetOptionNames());
  for (i = 0; i < key.OptionCount; i++)
  {
    valueName = key.GetOptionName(i);
    Log.Message(valueName + ": " + key.GetOption(valueName, "not specified"));
  }
  Log.PopLogFolder();
}

Python

def ListSystemFolders():

  key = Storages.Registry("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", HKEY_CURRENT_USER)

  Log.AppendFolder("List of system and special folders:", key.GetOptionNames())
  for i in range (0, key.OptionCount):
    valueName = key.GetOptionName(i)
    Log.Message(valueName + ": " + str(key.GetOption(valueName, "not specified")))
  Log.PopLogFolder()

VBScript

Sub ListSystemFolders
  Dim key, valueName, i

  Set key = Storages.Registry("Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders", HKEY_CURRENT_USER)

  Call Log.AppendFolder("List of system and special folders:", key.GetOptionNames)
  For i = 0 To key.OptionCount-1
    valueName = key.GetOptionName(i)
    Log.Message(valueName & ": " & key.GetOption(valueName, "not specified"))
  Next
  Log.PopLogFolder
End Sub

DelphiScript

procedure ListSystemFolders;
var key, valueName, i : OleVariant;
begin
  key := Storages.Registry('Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders', HKEY_CURRENT_USER);

  Log.AppendFolder('List of system and special folders:', key.GetOptionNames);
  for i := 0 to key.OptionCount-1 do
  begin
    valueName := key.GetOptionName(i);
    Log.Message(valueName + ': ' + key.GetOption(valueName, 'not specified'));
  end;
  Log.PopLogFolder;
end;

C++Script, C#Script

function ListSystemFolders()
{
  var key, valueName, i;

  key = Storages["Registry"]("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", HKEY_CURRENT_USER);

  Log["AppendFolder"]("List of system and special folders:", key["GetOptionNames"]());
  for (i = 0; i < key["OptionCount"]; i++)
  {
    valueName = key["GetOptionName"](i);
    Log["Message"](valueName + ": " + key["GetOption"](valueName, "not specified"));
  }
  Log["PopLogFolder"]();
}

To retrieve the option’s data marked as (Default) in the Windows registry editor, use an empty string as the option name:

JavaScript, JScript

// Logs the name of Windows startup sound
function GetSystemStartSound ()
{
  var key = Storages.Registry ("AppEvents\\Schemes\\Apps\\.Default\\SystemStart\\.Current", HKEY_CURRENT_USER);

  Log.Message (key.GetOption("", "not set"));
}

Python

# Logs the name of Windows startup sound
def GetSystemStartSound ():
  key = Storages.Registry("AppEvents\\Schemes\\Apps\\.Default\\SystemStart\\.Current", HKEY_CURRENT_USER)
  Log.Message(str(key.GetOption("", "not set")))

VBScript

' Logs the name of Windows startup sound
Sub GetSystemStartSound
  Dim key

  Set key = Storages.Registry ("AppEvents\Schemes\Apps\.Default\SystemStart\.Current", HKEY_CURRENT_USER)

  Log.Message (key.GetOption("", "not set"))
End Sub

DelphiScript

// Logs the name of Windows startup sound
procedure GetSystemStartSound;
var key : OleVariant;
begin
  key := Storages.Registry ('AppEvents\Schemes\Apps\.Default\SystemStart\.Current', HKEY_CURRENT_USER);

  Log.Message (key.GetOption('', 'not set'));
end;

C++Script, C#Script

// Logs the name of Windows startup sound
function GetSystemStartSound ()
{
  var key = Storages["Registry"]("AppEvents\\Schemes\\Apps\\.Default\\SystemStart\\.Current", HKEY_CURRENT_USER);

  Log["Message"](key["GetOption"]("", "not set"));
}

See Also

Storages Object
Registry Method

Highlight search results