Settings (gb.settings)
This class manages global configuration files.
This class can be used like an object by creating a hidden instance on demand.
This class is
creatable.
This class acts like a
read /
write array.
Static properties
DefaultDir
|
This property returns the default directory where settings configuration files are stored.
|
Static methods
FromString
|
Return a value from its settings internal string representation
|
ToString
|
Convert a value into its settings internal string representation
|
Properties
Count
|
Return the number of entries in the settings file
|
Keys
|
Return a virtual object used for enumerating all the keys used by the setting file.
|
Path
|
Return the path of the setting file.
|
Methods
Clear
|
Clears all settings.
|
Exist
|
Return if the specified settings key has a value
|
Read
|
Initializes the specified object from the settings file.
|
Reload
|
Cancels all settings changes, by reloading the settings file.
|
Save
|
Saves the configuration file to the disk.
|
Write
|
Writes the settings of the specified object in the settings file.
|
If you use this class statically, then the default project configuration file will be used.
The path of the default configuration file is:
User.Home &/ ".config/gambas3" &/ Application.Name & ".conf"
For example, the development environment configuration file is stored in
~/.config/gambas3/gambas3.conf
This example loads a window's position from a settings file when a form is opened. If the settings file does not exist, then the form's default position would be used. The example also saves the forms position when the form closes. The default settings file for the application is used.
Examples
PUBLIC SUB Form_Open()
' Update window position from settings file
' If the settings file is not found then
' use default position.
ME.Top = Settings["Window/Top", ME.Top]
ME.Left = Settings["Window/Left", ME.Left]
ME.Height = Settings["Window/Height", ME.Height]
ME.Width = Settings["Window/Width", ME.Width]
END
PUBLIC SUB Form_Close()
' Save window settings when application closes
Settings["Window/Top"] = ME.Top
Settings["Window/Left"] = ME.Left
Settings["Window/Height"] = ME.Height
Settings["Window/Width"] = ME.Width
END
The saved settings file would look something like the following settings in the Window group.
[Window]
Top=13
Left=18
Height=154
Width=235
PUBLIC SUB ShowASpecialSetting()
DIM prhOtherConfFile as Settings
DIM prhLangUsed as String
' Read a setting from a file used by another application
' located in /home/user/.config/otherapp/
' and named "international.conf"
'First create a new instance of Settings class
'with the full path to desired file
prhOtherConfFile = NEW Settings("/home/user/.config/otherapp/international.conf")
'next read the "Lang1" value from the "UserPreferences" section
prhLangUsed = prhOtherConfFile["UserPreferences/Lang1", NULL]
IF IsNull(prhLangUsed) THEN
Message.Info("No Lang1 found in conf file (or no Section named UserPreferences)")
ELSE
Message.Info("The value of Lang1 that found in conf file is:\n" & prhLangUsed)
END IF
END
Note that you can directly use the
Write method to store the settings of a window, and the
Read method to recall them.
Warning.
Shutting computer down
When you close your program the settings are automatically saved but if your program is running in the background and your computer is shutdown it will
hard exit and
not save the settings. So if your program is likely to still be running during the system shutdown and not closed manually then you should use the
Save function to save the settings
as they are changed and not leave it for when the program exits normally (as it won't exit normally)
Using QUIT to exit
The same is said if you use
QUIT to exit your program.
This will kill the program process immediately before the Settings save so again you must use Settings.
Save before using
QUIT.