Results 1 to 11 of 11

Thread: Deleting from an INI file.

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2001
    Location
    NJ, USA
    Posts
    12

    Unhappy Deleting from an INI file.

    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?

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    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.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2001
    Location
    NJ, USA
    Posts
    12
    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.

  4. #4
    New Member
    Join Date
    Jan 2002
    Location
    Sweden
    Posts
    8
    I suggest you work with the registery instead, there are two useful function built-in VB (atleast my version, VB6)

    GetSetting()
    SaveSetting()

    the parameters that will show up are pretty self-explantory.

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    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.

  6. #6

    Thread Starter
    New Member
    Join Date
    Jul 2001
    Location
    NJ, USA
    Posts
    12
    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..

  7. #7
    New Member
    Join Date
    Jan 2002
    Location
    Sweden
    Posts
    8
    roget that.

    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

  8. #8
    New Member
    Join Date
    Jan 2002
    Location
    Sweden
    Posts
    8
    sorry lost what the topic was all about.

    heres some code (not tested) I just wrote:


    function RemoveSection(strName)

    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

    end function

  9. #9
    ALFWare
    Guest

    Wink Another Option

    Another option is to use a FREE third party library to do it.

    http://sevillaonline.com/ActiveX/ARINIManLib.htm

    It is a very simple to use library, and it is free. Thought I admit it sucks having to add another file to your package but makes life easier!

    Hope that helps

  10. #10
    ALFWare
    Guest

    Thumbs up No Dependantcies way

    Or another option is to use this class which I got from VB Accelerator another excellent website.

    All you do is set the path and away you go.
    Attached Files Attached Files

  11. #11

    Thread Starter
    New Member
    Join Date
    Jul 2001
    Location
    NJ, USA
    Posts
    12

    Thumbs up

    Thanks a bunch guys.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width