TIni - INI File via WinAPI

Source: source/classes/ini.prg

TIni provides high-level access to Windows INI files through the Windows API functions (GetPrivateProfileString, WritePrivateProfileString, GetPrivateProfileSectionNames, etc.). It supports reading and writing string, numeric, date, and logical values, enumerating sections, and deleting sections or individual entries. The optional lAutoSet flag automatically saves changes on assignment.

DATA Members

DATATypeDescription
cIniFileCharacterFull path to the INI file
lAutoSetLogicalIf .T., changes are written to disk immediately

Methods

MethodDescription
New( cFile )Create TIni object pointing to the specified INI file
Get( cSec, cKey, uDef )Return the value of a key in a section, or uDef if not found (auto-detects type: string, number, date, logical)
Set( cSec, cKey, uVal )Write a value to a key in a section. Type is preserved (dates, numbers, logicals are converted)
Sections()Return an array of section names in the INI file
DelSection( cSec )Delete an entire section and all its keys
DelEntry( cSec, cKey )Delete a single key from a section

Example: Read/Write Settings

#include "FiveWin.ch"

function Main()

   local oIni := TIni():New( "C:\MyApp\settings.ini" )
   local cServer, nPort, lAutoSave, dLastRun

   // Read values with defaults
   cServer   := oIni:Get( "Database", "Server",  "localhost" )
   nPort     := oIni:Get( "Database", "Port",    3306 )
   lAutoSave := oIni:Get( "Database", "AutoSave", .F. )
   dLastRun  := oIni:Get( "Database", "LastRun",  Date() - 1 )

   ? "Server:",  cServer
   ? "Port:",    nPort
   ? "AutoSave:", lAutoSave
   ? "Last run:", dLastRun

   // Write updated values
   oIni:Set( "Database", "LastRun",   Date() )
   oIni:Set( "Database", "AutoSave",  .T. )

   // List all sections
   local aSecs := oIni:Sections()
   AEval( aSecs, {|c| ? "Section: " + c } )

return nil

See Also