Skip to content

Class: cIniFile

Properties | Events | Methods | Index of Classes

Class for processing Windows INI (configuration) files

Hierarchy

cObject > cIniProcessor > cIniFile

Show full hierarchy and direct subclasses

Library: Common Class Library

Package: cIniFile.pkg

Description

cIniProcessor is a newer class that processes INI files in memory. We recommend using it for new code. cIniFile is still maintained, but largely for backwards compatibility.

Use the cIniFile class if you want changes to be made directly on disk. The class reads and parses the file for every operation, and on changes, it writes it directly.

The cIniProcessor class reads and parses an ini file into a memory structure, and then its functions work on the data in memory. So SetValue only changes the value in memory, and not on disk. To write the changes, use WriteToFile. cIniProcessor will be more optimal when reading / writing many values. When using the cIniFile class, you do not have to worry about when to read / write the file.

We recommend not mixing usage of cIniFile and cIniProcessor class members.

Use ParseString or ReadFromFile to load ini data, use Stringify or WriteToFile to generate / save the data. Use functions like SetValue and GetValue to read / manipulate values.

An INI file is a text file that stores data in logical groups called sections. Each section can contain multiple keys. Each key stores a single value. Section names must be unique within the file and Key names must be unique within each Section.

The format of an INI file is as follows:

[SectionName]
Key=Value
Key=Value
..
[SectionName]
Key=Value
Key=Value
..

INI files are convenient for storing application data, as an alternative to the system Registry, because they are physical files that can easily be copied. An example of an INI file would be a "Workspace file" that DataFlex uses to store information about a Workspace. Unlike the Registry, though, data does not have a data-type associated with it, and all data is assumed to be of type String.

Prior to writing or reading data in an INI file, set its psFileName property. To write data, use the WriteString method and to read data, use the ReadString. If you write to a file that doesn't exist, it will be created. Before reading from an INI file, you should check that the Section/Key exists. To verify the existence of a Section, use SectionExists and for a Key, use KeyExists.

The ReadSections method is used to discover the names of all existing Sections. To discover the names of all Keys within a Section, use ReadSection.

To delete a Section, use DeleteSection, which will remove the Section and all Keys. To delete a Key, use DeleteKey.

Col 1 Col 2
Note: It is customary for INI files to have an ".ini" file extension, but this is not a requirement.

Sample

In this sample, the date is written to a Key called "Date" in Section called "Last Accessed" in an INI file called "Preferences.ini" that we want placed in the first path of the Workspace's DataPath.

Function PrimaryDataPath Returns String
    // Returns the first directory of the Workspace's DataPath
    String sDataPath sPrimaryDataPath

    Get psDataPath of (phoWorkspace(ghoApplication)) To sDataPath
    Get PathAtIndex of (phoWorkspace(ghoApplication)) sDataPath 1 To sPrimaryDataPath

    Function_Return sPrimaryDataPath
End_Function

Object oPreferences is a cIniFile
    Set psFileName To (PrimaryDataPath(self) +"\Preferences.ini")
    Send WriteString "Last Accessed" "Date" (CurrentDateTime())
End_Object

Sample

In this sample, the date is retrieved and displayed in a message box.

Object oPreferences is a cIniFile
    Procedure DoDisplayLastAccessed
        Boolean bAccessed
        String sDate

        Set psFileName To (PrimaryDataPath(self) +"\Preferences.ini")
        Get KeyExists "Last Accessed" "Date" To bAccessed
        If bAccessed Begin
            Get ReadString "Last Accessed" "Date" "" To sDate
            Send Info_Box ("Last access was: " + sDate)
        End
        Else Send Info_Box "You have not accessed the program before"
    End_Procedure
End_Object

See Also

cRegistry | Info_Box