Results 1 to 1 of 1

Thread: VB - Handy INI file manipulation sample

  1. #1

    Thread Starter
    Fanatic Member daydee's Avatar
    Join Date
    Jun 2001
    Location
    Canada
    Posts
    560

    VB - Handy INI file manipulation sample

    OK, here's a few handy Functions/Subs you can used for manipulating ini files.
    Some of which are undocumented!
    You can do more with ini files than this but this should account for the most often sought after anyway.
    Cheers!
    VB Code:
    1. 'this code to module
    2. Option Explicit
    3. Private 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
    4. Private 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
    5. Private Declare Function GetPrivateProfileSectionNames Lib "kernel32" Alias "GetPrivateProfileSectionNamesA" (ByVal lpSectionNames As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
    6. Private Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
    7. Private 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
    8. Private 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
    9.  
    10. Public Function ReadFromINI(ByVal strSection As String, ByVal strkey As String, ByVal strfullpath As String, Optional ByVal strDefault As String = "") As String
    11.    'function to return the key value of any keys inside an ini section.
    12.    Dim strBuffer As String
    13.    Let strBuffer$ = String$(750, Chr$(0&))
    14.    Let ReadFromINI$ = Left$(strBuffer$, GetPrivateProfileString(strSection$, ByVal LCase$(strkey$), strDefault, strBuffer, Len(strBuffer), strfullpath$))
    15. End Function
    16.  
    17. Public Sub WriteToINI(ByVal strSection As String, ByVal strkey As String, ByVal strkeyvalue As String, ByVal strfullpath As String)
    18.    'sub to write a key and its value inside an ini section.
    19.    Call WritePrivateProfileString(strSection$, UCase$(strkey$), strkeyvalue$, strfullpath$)
    20. End Sub
    21.  
    22. Public Sub DeleteIniSection(ByVal strSection As String, ByVal strfullpath As String)
    23.    'sub to delete an entire ini section.
    24.    Call WritePrivateProfileString(strSection, 0&, 0&, strfullpath)
    25. End Sub
    26.  
    27. Public Sub DeleteIniKey(ByVal strSection As String, ByVal strKeyname As String, ByVal strfullpath As String)
    28.    'sub to delete a particular key inside an ini section.
    29.    Call WritePrivateProfileString(strSection, strKeyname, 0&, strfullpath)
    30. End Sub
    31.  
    32. Public Function CheckIfIniKeyExists(ByVal strSection, ByVal strKeyname As String, ByVal strfullpath As String) As Boolean
    33.    'function to check if an ini key exists.
    34.    Dim str_A As String, str_B As String
    35.    str_A = ReadFromINI(strSection, strKeyname, strfullpath, "A")
    36.    str_B = ReadFromINI(strSection, strKeyname, strfullpath, "B")
    37.    If str_A = str_B Then CheckIfIniKeyExists = True
    38. End Function
    39.  
    40. Public Function CheckIfIniSectionExists(ByVal strSection As String, ByVal strfullpath As String) As Boolean
    41.    'function to check if an ini section exists.
    42.    Dim strBuffer As String
    43.    Let strBuffer$ = String$(750, Chr$(0&))
    44.    CheckIfIniSectionExists = CBool(GetPrivateProfileSection(strSection$, strBuffer, Len(strBuffer), strfullpath$) > 0)
    45. End Function
    46.  
    47. Public Function GetLongFromINI(ByVal strSection, ByVal strKeyname As String, ByVal strfullpath As String, Optional ByVal lngDefault As Long = 0) As Long
    48.    'function to return the Long portion of a key value. (will return 0 if the optional argument has not been passed and key value is non numeric or if key does not exist or is empty)
    49.    GetLongFromINI = GetPrivateProfileInt(strSection, strKeyname, lngDefault, strfullpath)
    50. End Function
    51.  
    52. Public Sub RenameIniKey(ByVal strSection As String, ByVal strKeyname As String, ByVal strNewKeyname, ByVal strfullpath As String)
    53.    'sub to rename a particular key inside an ini section.
    54.    Dim tmpKeyValue As String
    55.    If CheckIfIniKeyExists(strSection, strKeyname, strfullpath) = False Then Exit Sub
    56.    tmpKeyValue = ReadFromINI(strSection, strKeyname, strfullpath)
    57.    Call WriteToINI(strSection, strNewKeyname, tmpKeyValue, strfullpath)
    58.    Call DeleteIniKey(strSection, strKeyname, strfullpath)
    59. End Sub
    60.  
    61. Public Sub RenameIniSection(ByVal strSection As String, ByVal strNewSection As String, ByVal strfullpath As String)
    62.   'sub to rename an ini section name.
    63.   Dim KeyAndVal() As String, Key_Val() As String, strBuffer As String
    64.   Dim intx As Integer
    65.   Let strBuffer$ = String$(750, Chr$(0&))
    66.     Call GetPrivateProfileSection(strSection, strBuffer, Len(strBuffer), strfullpath)
    67.       KeyAndVal = Split(strBuffer, vbNullChar)
    68.         For intx = LBound(KeyAndVal) To UBound(KeyAndVal)
    69.           Key_Val = Split(KeyAndVal(intx), "=")
    70.            If UBound(Key_Val) = -1 Then Exit For
    71.           WriteToINI strNewSection, Key_Val(0), Key_Val(1), strfullpath
    72.          Next
    73.        DeleteIniSection strSection, strfullpath
    74.      Erase KeyAndVal: Erase Key_Val
    75. End Sub
    76.  
    77. Public Sub LoadIniSectionsLB(ByVal lstB As ListBox, ByVal strfullpath As String)
    78.   'sub to load all of the ini section names into a listbox.
    79.   Dim sectnNames() As String, strBuffer As String
    80.   Dim intx As Integer
    81.   Let strBuffer$ = String$(750, Chr$(0&))
    82.     Call GetPrivateProfileSectionNames(strBuffer, Len(strBuffer), strfullpath)
    83.       sectnNames = Split(strBuffer, vbNullChar)
    84.         For intx = LBound(sectnNames) To UBound(sectnNames)
    85.           If sectnNames(intx) = vbNullString Then Exit For
    86.          lstB.AddItem sectnNames(intx)
    87.         Next
    88.       'If lstB.ListCount > 0 Then lst.Selected(0) = True '<<--if you want first list item in listbox selected
    89.     Erase sectnNames
    90. End Sub
    91.  
    92. Public Function LoadIniSectionsArray(ByVal strfullpath As String) As String()
    93.   'function for populating array with all ini section names.
    94.   Dim sectnNames() As String, strBuffer As String
    95.   Let strBuffer$ = Space(1024)
    96.     Call GetPrivateProfileSectionNames(strBuffer, Len(strBuffer), strfullpath)
    97.       sectnNames = Split(strBuffer, vbNullChar)
    98.     LoadIniSectionsArray = Split(strBuffer, vbNullChar, UBound(sectnNames) - 1) 'vbLf
    99.   Erase sectnNames
    100. End Function
    101.  
    102. Public Sub LoadIniSectionKeysLB(ByVal strSection As String, ByVal lstB As ListBox, ByVal strfullpath As String)
    103.    'sub to load all keys from an ini section into a listbox.
    104.    Dim KeyAndVal() As String, Key_Val() As String, strBuffer As String
    105.    Dim intx As Integer
    106.    Let strBuffer$ = String$(750, Chr$(0&))
    107.      Call GetPrivateProfileSection(strSection, strBuffer, Len(strBuffer), strfullpath)
    108.        KeyAndVal = Split(strBuffer, vbNullChar)
    109.          For intx = LBound(KeyAndVal) To UBound(KeyAndVal)
    110.            If KeyAndVal(intx) = vbNullString Then Exit For
    111.              Key_Val = Split(KeyAndVal(intx), "=")
    112.                If UBound(Key_Val) = -1 Then Exit For
    113.              lstB.AddItem Key_Val(0) '<--to get the keys prior to "=" delimiter only
    114.           'lstB.additem inikey(1) '<--to get the key values past the "=" delimiter only
    115.          Next
    116.        'If lstB.ListCount > 0 Then lst.Selected(0) = True '<<--if you want first list item in listbox selected
    117.      Erase KeyAndVal: Erase Key_Val
    118. End Sub
    119.  
    120. Public Function GetSectionKeyCount(ByVal strSection As String, ByVal strfullpath As String) As Integer
    121.   'function to get the key count of a particular ini section.
    122.   Dim KeyAndVal() As String, strBuffer As String
    123.   Dim intx As Integer, SectionKeyCount As Integer
    124.   Let strBuffer$ = String$(750, Chr$(0&))
    125.     Call GetPrivateProfileSection(strSection, strBuffer, Len(strBuffer), strfullpath)
    126.       KeyAndVal = Split(strBuffer, vbNullChar)
    127.         For intx = LBound(KeyAndVal) To UBound(KeyAndVal)
    128.           If KeyAndVal(intx) = vbNullString Then Exit For
    129.           SectionKeyCount = SectionKeyCount + 1
    130.         Next
    131.       GetSectionKeyCount = SectionKeyCount
    132.     Erase KeyAndVal
    133. End Function
    Edit note: added a few more subs and improved on what I had already posted :)
    Last edited by daydee; Apr 8th, 2003 at 11:16 AM.
    Give your music collection a whole new life with PartyTime Jukebox

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