Results 1 to 5 of 5

Thread: Reading INI files

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2000
    Posts
    4
    I ahve recenlty downloaded an sample project to read ini files.

    What I would like to be able to do is populate various list boxes or combo boxes with the various oprions in the ini file.

    for example I would firstly like to get a list of the sections in brackets, then after selecting this a list of the parameter under that section.

    Is this at all possible.

    Thnx for any help you can extend.

    ~qb~
    ~QUBi~

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    If you're going to do it that way, it's probably easier to read in the whole file and parse it yourself, that way you can instantly get a tree-based structure representing the file.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3
    Guest
    As parksie suggested, that is probably a faster way to do it and here's how:

    Code:
    Public Sub List_Add(List As ComboBox, txt As String)
    List.AddItem txt
    End Sub
    
    Public Sub List_Load(TheList As ListBox, FileName As String)
    On Error Resume Next
    Dim TheContents As String
    Dim fFile As Integer
    fFile = FreeFile
     Open FileName For Input As fFile
       Do
         Line Input #fFile, TheContents$
            Call List_Add(TheList, TheContents$)
       Loop Until EOF(fFile)
     Close fFile
    End Sub
    
    
    Public Sub List_Save(TheList As ListBox, FileName As String)
    On Error Resume Next
    Dim Save As Long
    Dim fFile As Integer
    fFile = FreeFile
    Open FileName For Output As fFile
       For Save = 0 To TheList.ListCount - 1
          Print #fFile, TheList.List(Save)
       Next Save
    Close fFile
    End Sub
    But here is how to read an ini file anyway.

    Code:
    Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
    
    Public Function ReadINI(strsection As String, strkey As String, strfullpath As String) As String
       Dim strbuffer As String
       Let strbuffer$ = String$(750, Chr$(0&))
       Let ReadINI$ = Left$(strbuffer$, GetPrivateProfileString(strsection$, ByVal LCase$(strkey$), "", strbuffer, Len(strbuffer), strfullpath$))
    End Function
    
    Usage:
    
    'x = readini("test.ini", "test", "C:\test.ini")
    'List1.Additem x

  4. #4
    Guest
    See this tip

  5. #5
    Hyperactive Member
    Join Date
    Jan 1999
    Location
    Rotterdam, Netherlands
    Posts
    386
    Here's an ini class I wrote a time ago:
    http://codeguru.earthweb.com/vb/articles/1772.shtml
    Hope this helps

    Crazy D

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