|
Post by Mark Dunham on Jun 18, 2019 14:16:42 GMT -5
Does anyone have a good example of loading settings from an .ini file? Or is this deemed outdated at this point. I can load settings from text files, and files with extension that I create but wondered if there was a standard way I am not thinking of. Case in point I have program that plays music if I set the music off the next time I run that program I would like it to still have the music off.
|
|
ntech
New Member
Posts: 49
|
Post by ntech on Jun 18, 2019 15:19:35 GMT -5
Well, most of the .ini files are in this form:
this = that
So for your program, it'd be
musicOff=True
To load it:
global musicOff
open "settings.ini" for input as #i
while eof(#i) = 0
line input #i, str$ call process$ str$
wend
close #i
print "The music is " + boolToTxt$(musicOff)
function boolToTxt$(int)
if int <> 0 then
boolToTxt$ = "True"
else
boolToTxt$ = "False"
end if
end function
sub process$ arg$
if word$(arg$, 1, "=") = "musicOff" then
if word(arg$, 2, "=") = "True" then
musicOff = 1
else
musicOff = 0
end if
end if
end sub
|
|
|
Post by Chris Iverson on Jun 18, 2019 15:46:22 GMT -5
While MS tends to recommend the registry for centralization purposes, there's nothing wrong with using INI files, and in fact, Windows still has built-in APIs to simplify working with them.
'Creates an INI file with this format: ' '[section] 'key=value ' 'Actual contents of file created: ' '[sample] 'SomeParameter=MyData
section$ = "sample" key$ = "SomeParameter" value$ = "MyData" iniFile$ = "test.ini"
a = WriteINIData(iniFile$, section$, key$, value$)
print ReadINIData$(iniFile$, section$, key$)
Function ReadINIData$(iniFile$, section$, key$) bufSize = 1024 buf$ = space$(bufSize)
CallDLL #kernel32, "GetPrivateProfileStringA",_ section$ as ptr,_ key$ as ptr,_ 0 as ulong,_ buf$ as ptr,_ bufSize as long,_ iniFile$ as ptr,_ ret as long
ReadINIData$ = left$(buf$, ret) End Function
Function WriteINIData(iniFile$, section$, key$, data$) CallDLL #kernel32, "WritePrivateProfileStringA",_ section$ as ptr,_ key$ as ptr,_ data$ as ptr,_ iniFile$ as ptr,_ WriteINIData as long End Function
|
|
|
Post by Mark Dunham on Jun 18, 2019 15:53:17 GMT -5
Great examples I will have to see what I can work up. Appreciate it.
|
|