Gestion de fichiers ini avancée

Contenu du snippet

Ce code permet de lire des fichiers ini et d'éffectuer des opérations avancées comme:
la suppression de clées
la supressions de sections
l'énumération des sections
l'énumération des clées d'une section (nom + (=valeur optionnelle)
l'écriture manuelle d'une section

Le fonctionnement est très simple et je ne crois pas avoir besoins de donner des explications ;-). Dîtes-moi le si vous en voullez.

Source / Exemple :


Option Explicit
Private Declare Function GetPrivateProfileSection Lib "kernel32.dll" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Declare Function GetPrivateProfileSectionNames Lib "kernel32.dll" Alias "GetPrivateProfileSectionNamesA" (ByVal lpszReturnBuffer As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Declare Function GetPrivateProfileString Lib "kernel32.dll" 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
Private Declare Function WritePrivateProfileSection Lib "kernel32.dll" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
Private Declare Function WritePrivateProfileString Lib "kernel32.dll" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long

Public Function DeleteSection(ByVal strINIFile As String, ByVal strSection As String) As Boolean
If WritePrivateProfileSection(strSection, Chr$(0), strINIFile) <> 0 Then DeleteSection = True
End Function

Public Function DeleteKey(ByVal strINIFile As String, ByVal strSection As String, ByVal strKey As String) As Boolean
If WritePrivateProfileString(strSection, strKey, Chr$(0), strINIFile) <> 0 Then DeleteKey = True
End Function

Public Function GetSectionsCollection(ByVal strINIFile As String) As Collection
Dim strBuf As String, col As Variant, bufCol As Collection, I As Long, lngRet As Long
strBuf = String$(500, 0)
lngRet = GetPrivateProfileSectionNames(strBuf, Len(strBuf), strINIFile)
If lngRet <> 0 Then strBuf = Left$(strBuf, lngRet) Else: Exit Function
col = Split(strBuf, Chr$(0))
Set bufCol = New Collection
For I = 0 To UBound(col)
If Trim$(col(I)) <> Empty Then bufCol.Add col(I)
Next
Set col = Nothing
Set GetSectionsCollection = bufCol
Set bufCol = Nothing
End Function

Public Function GetKeysCollection(ByVal strINIFile As String, ByVal strSection As String, Optional ByVal bWithValues As Boolean = True) As Collection
Dim strBuf As String, col As Variant, Col2 As Variant, bufCol As Collection, I As Long, lngRet As Long
strBuf = String$(500, 0)
lngRet = GetPrivateProfileSection(strSection, strBuf, Len(strBuf), strINIFile)
If lngRet <> 0 Then strBuf = Left$(strBuf, lngRet) Else: Exit Function
col = Split(strBuf, Chr$(0))
Set bufCol = New Collection
For I = 0 To UBound(col)
If Trim$(col(I)) <> Empty Then
    If Left$(col(I), 1) <> ";" Then
        If bWithValues = True Then
        bufCol.Add col(I)
        Else
        Col2 = Split(col(I), "=", 2)
        bufCol.Add Col2(0)
        End If
    End If
End If
Next
Set col = Nothing
Set Col2 = Nothing
Set GetKeysCollection = bufCol
Set bufCol = Nothing
End Function

Public Function WriteINISection(ByVal strINIFile As String, ByVal strSection As String, ByVal strKeysAndVals As String) As Boolean
If WritePrivateProfileSection(strSection, strKeysAndVals, strINIFile) <> 0 Then WriteINISection = True
End Function

Public Function WriteINIKeyValue(ByVal strINIFile As String, ByVal strSection As String, ByVal strKey As String, ByVal strValue As String) As Boolean
If WritePrivateProfileString(strSection, strKey, strValue, strINIFile) <> 0 Then WriteINIKeyValue = True
End Function

Public Function GetINIKeyValue(ByVal strINIFile As String, ByVal strSection As String, ByVal strKey As String) As String
Dim strBuf As String, lngRet As Long
strBuf = String$(255, 0)
lngRet = GetPrivateProfileString(strSection, strKey, "", strBuf, Len(strBuf), strINIFile)
If lngRet <> 0 Then GetINIKeyValue = Left$(strBuf, lngRet)
End Function

Conclusion :


Alors?

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.