I'm able to readto/writefrom INI files, but I'm also looking for a way to delete lines from it, i.e. being able to remove a [Section] or Value. Anyone know how?
I haven't used InI files in several years, but back in the day when I did, I used to read the InI file into a text box on a Admin screen (InI files are just text files away). Delete what I didn't want, and then write the contents of the text box back to my InI file name. It probably wasn't the most efficient way of doing it, but it always worked.
Originally posted by Slayer-X
Hmm, well what I was wanting to do is be able to have the program delete it automatically without the user really doing anything to it.
Agree. Users should not be updating InI files, which is why I restricted this feature to Admin screens only. mistbringer's idea of using the registry is much cleaner than dealing with InI files and it is something that most users won't be able to muck up.
I was trying to avoid having to use the Registry because I plan on saving a lot of information and I don't want to have to stick it all in the registry..
but its not that diffrent to create functions that can Add a section, Remove a section, add a variable=value to a specific Section.
There are WinAPI functions for the above though.
And they do exactly what they say. A function for deleting sections you have to write yourself. Pretty easy.
Public Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Public Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Public Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Long, ByVal lpFileName As String) As Long
open "true.ini" for input as #1
open "temp.ini" for output as #2
do while not eof(1)
line input #1, strLineData
strTLineData = ltrim(rtrim(strLineData))
'Look for sections
if left(strTLineData,1) = "[" and right(strTLienData,1) = "]" then
FoundSection = False
'Look for the wanted section
if ucase(mid(strTLineData,2,len(strTLineData)-2)) = strName then
'Found it
FoundSection = TRUE
end if
end if
'Write lines that doesnt include the section we want to delete
if FoundSection = FALSE then print #2, strLineData & vbcrlf
loop
close #1
close #1
kill "true.ini"
ren "temp.ini" as "true.ini" 'I dont remember if this is the correct syntax