Page 1 of 1

updating preferences key/values

Posted: Fri Sep 01, 2023 8:49 am
by TinjUngle
new to purebasic, and i am struggling to work out how to update key values in a preferences file.

when i use the following to create the preference file with some groups and keys/values

Code: Select all

Procedure createPrefs()
    CreatePreferences(GetCurrentDirectory()+"Preferences.prefs")
    PreferenceGroup("Global")
    WritePreferenceString("ApplicationName", "maya Batch")
    PreferenceGroup("Paths")
    WritePreferenceString("mayaPath", "")
  ClosePreferences()
EndProcedure
the preferences file has the following content
[Global]
ApplicationName = maya Batch
[Paths]
mayaPath =

then if i update one of the keys values in the paths group - using the following

Code: Select all

Procedure updatePrefs()
    If CreatePreferences(GetCurrentDirectory()+"Preferences.prefs")
      PreferenceGroup("Paths")
      WritePreferenceString("mayaPath", mayaPath$)
      ClosePreferences()   
    EndIf  
EndProcedure
the preference file is updated to the following content
[Paths]
mayaPath = C:\Program Files\Autodesk\Maya2019\bin\

it is blowing away the other group [Global] and its keys, also if i have more than 1 key in the paths group and update one of the keys the other key gets blown away as well

i have only been using purebasic for a couple of hours so its probably a newbie issue - but would liek to get some help on were i am going wrong.

cheers
Arthru


// Code Tags added (Kiffi)

Re: updating preferences key/values

Posted: Fri Sep 01, 2023 9:13 am
by Janni
Hi and welcome to this forum and as a new PB user :)

I would skip CreatePreferences in your updatePrefs() procedure. Use OpenPreferences instead.
Documentation for CreatePreferences states: Creates a new empty preference file. If the file already exists, the file is erased.

PS! use the

Code: Select all

code
tag for easier reading.

Re: updating preferences key/values

Posted: Fri Sep 01, 2023 11:12 am
by Jeromyal
Syntax

Result = CreatePreferences(Filename$ [, Flags])
Description

Creates a new empty preference file. If the file already exists, the file is erased.

One of the best ways to use the preference file routines is to set all the variables you are using with the ReadPreference commands at the start.
Then a procedure to create and optionally format your file if it does not exist and write out your variables.
You could do...

Code: Select all

; start of application.
OpenPreferences(GetCurrentDirectory() + "Preferences.prefs") ; pass or fail the readpreference commands will populate your variables.
PreferenceGroup("Global")
Global info$ = ReadPreferenceString("Global Option 1", "default")
  ; ...
ClosePreferences()

; Later in your application if we want to save out the data we have in various global variables

Procedure SavePreferences() ; call when we are closing application or if we realy need to update preference file.
  If FileSize(GetCurrentDirectory() + "Preferences.prefs") - 1 ; Does not exist.
    If CreatePreferences(GetCurrentDirectory() + "Preferences.prefs")
      ; optional
    ; here you could format your initalization file with a structure that gives you control of how it looks in a text editor.
      PreferenceComment("Option to pre-populate your file with comments groups and keys. This comment serves as a header of the file.")
      ; ...
      ; endoptional
    Else
      ; Something went wrong! is the location writable?
    EndIf
    ClosePreferences()
  EndIf
  If OpenPreferences(GetCurrentDirectory() + "Preferences.prefs")
    PreferenceGroup("Global")
    WritePreferenceString("Global Option 1", info$)
    ClosePreferences()
  EndIf
EndProcedure
[/code]

Re: updating preferences key/values - solved

Posted: Fri Sep 01, 2023 11:36 am
by TinjUngle
thanks Jeromyal for the code, really useful. and up and working now.

great forum and look forward to getting into PB some more

cheers
Arthur