Storages.Binary Method

Applies to TestComplete 15.48, last modified on March 01, 2023

Description

Use the Binary method to open the specified binary file and to get the FileSection object for its root section. If the specified file does not exist, Binary creates it in memory. You can save it on the disk later by calling the FileSection.Save or FileSection.SaveAs methods.

Declaration

Storages.Binary(FileName)

FileName [in]    Required    String    
Result A FileSection object

Applies To

The method is applied to the following object:

Parameters

The method has the following parameter:

FileName

Specifies the full path to the desired binary file.

Result Value

A FileSection object.

Remarks

Binary can open only those files that were created by this method. (They have a specific structure.) If the file was not created by Binary, an exception occurs.

Example

The following example demonstrates how to open the specified binary file, modify the option value stored in it and save the changes.

JavaScript, JScript

function Test()
{

  var Path, FileSection, SectionName, OptionName, Section, NewValue;
  // Specifies the path to the binary file
  Path = "D:\\Work Folder\\BinaryFile.bin";
  // Loads data from the binary file
  FileSection = Storages.Binary(Path);

  SectionName = "Section1";
  // Specifies the name of the option whose value we want to change
  OptionName = "Option1";
  // Specifies a new value for the option
  NewValue = "New Value";

  // Obtains the section by its name
  Section = FileSection.GetSubSection(SectionName);

  // Checks if the option belongs to the obtained section
  if (Section.OptionExists(OptionName))
    {
    // Sets a new value for the option
    Section.SetOption(OptionName, NewValue);
    Log.Message("The " + OptionName + " option value has been modified");
    // Saves the changes
    FileSection.Save();
    }
  else
    Log.Warning("The " + OptionName + " option does not exist");

}

Python

def Test():

  # Specifies the path to the binary file
  Path = "D:\\Work Folder\\BinaryFile.bin"
  # Loads data from the binary file
  FileSection = Storages.Binary(Path)

  SectionName = "Section1"
  # Specifies the name of the option whose value we want to change
  OptionName = "Option1"
  # Specifies a new value for the option
  NewValue = "New Value"

  # Obtains the section by its name
  Section = FileSection.GetSubSection(SectionName)

  # Checks if the option belongs to the obtained section
  if (Section.OptionExists(OptionName)):
    # Sets a new value for the option
    Section.SetOption(OptionName, NewValue)
    Log.Message("The " + OptionName + " option value has been modified")
    # Saves the changes
    FileSection.Save()
  else:
    Log.Warning("The " + OptionName + " option does not exist")

VBScript

Sub Test

  Dim Path, FileSection, SectionName, OptionName, Section, NewValue
  ' Specifies the path to the binary file
  Path = "D:\Work Folder\BinaryFile.bin"
  ' Loads data from the binary file
  Set FileSection = Storages.Binary(Path)

  SectionName = "Section1"
  ' Specifies the name of the option whose value we want to change
  OptionName = "Option1"
  ' Specifies a new value for the option
  NewValue = "New Value"

  ' Obtains the section by its name
  Set Section = FileSection.GetSubSection(SectionName)

  ' Checks if the option belongs to the obtained section
  If Section.OptionExists(OptionName) Then
    ' Sets a new value for the option
    Call Section.SetOption(OptionName, NewValue)
    Log.Message("The " & OptionName & " option value has been modified")
    ' Saves the changes
    FileSection.Save
  Else
    Log.Warning("The " & OptionName & " option does not exist")
  End If

End Sub

DelphiScript

procedure Test();
var Path, FileSection, SectionName, OptionName, Section, NewValue;
begin

  // Specifies the path to the binary file
  Path := 'D:\Work Folder\BinaryFile.bin';
  // Loads data from the binary file
  FileSection := Storages.Binary(Path);

  SectionName := 'Section1';
  // Specifies the name of the option whose value we want to change
  OptionName := 'Option1';
  // Specifies a new value for the option
  NewValue := 'New Value';

  // Obtains the section by its name
  Section := FileSection.GetSubSection(SectionName);

  // Checks if the option belongs to the obtained section
  if Section.OptionExists(OptionName) then
    begin
    // Sets a new value for the option
    Section.SetOption(OptionName, NewValue);
    Log.Message('The ' + OptionName + ' option value has been modified');
    // Saves the changes
    FileSection.Save;
    end
  else
    Log.Warning('The ' + OptionName + ' option does not exist');

end;

C++Script, C#Script

function Test()
{

  var Path, FileSection, SectionName, OptionName, Section, NewValue;
  // Specifies the path to the binary file
  Path = "D:\\Work Folder\\BinaryFile.bin";
  // Loads data from the binary file
  FileSection = Storages["Binary"](Path);

  SectionName = "Section1";
  // Specifies the name of the option whose value we want to change
  OptionName = "Option1";
  // Specifies a new value for the option
  NewValue = "New Value";

  // Obtains the section by its name
  Section = FileSection["GetSubSection"](SectionName);

  // Checks if the option belongs to the obtained section
  if (Section["OptionExists"](OptionName))
    {
    // Sets a new value for the option
    Section["SetOption"](OptionName, NewValue);
    Log["Message"]("The " + OptionName + " option value has been modified");
    // Saves the changes
    FileSection["Save"]();
    }
  else
    Log["Warning"]("The " + OptionName + " option does not exist");

}

See Also

FileSection Object
INI Method
Registry Method
XML Method

Highlight search results