Module ModINI
'Nom du fichier INI
Dim Fichier As String = Application.StartupPath & "\Backup.ini"
'Constante
Const MAX_ENTRY As Integer = 32768
#Region "Lire INI"
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPr" & _
"PivateProfileStringA" (ByVal lpApplicationName As String, ByVal _
lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As System.Text. _
StringBuilder, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
Function LireINI(ByVal Entete As String, ByVal Variable As String) As String
Dim defval As String = ""
Try
Dim StrBuild As New System.Text.StringBuilder(MAX_ENTRY)
Dim Ret As Integer = GetPrivateProfileString(Entete, Variable, _
defval, StrBuild, MAX_ENTRY, Fichier)
Return StrBuild.ToString
Catch
Return defval
End Try
End Function
#End Region
#Region "Ecrire INI"
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "Wri" & _
"tePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal _
lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
Function EcrireINI(ByVal Entete As String, ByVal Variable As String, ByVal _
Valeur As String)
WritePrivateProfileString(Entete, Variable, Valeur, Fichier)
End Function
#End Region
#Region "Supprime INI"
Function SupprimeINI(ByVal Entete As String, Optional ByVal Variable As _
String = Nothing)
WritePrivateProfileString(Entete, Variable, vbNullString, Fichier)
End Function
#End Region
#Region "Sections INI"
Private Declare Function GetPrivateProfileSectionNames Lib "kernel32.dll" _
Alias "GetPrivateProfileSectionNamesA" (ByVal lpszReturnBuffer() As Byte, ByVal _
nSize As Integer, ByVal lpFileName As String) As Integer
Function SectionsINI() As ArrayList
SectionsINI = New ArrayList
Dim Buffer(MAX_ENTRY) As Byte
Dim BuffStr As String
Dim PrevPos As Integer = 0
Dim Length As Integer
Try
Length = GetPrivateProfileSectionNames(Buffer, MAX_ENTRY, Fichier)
Catch
Exit Function
End Try
Dim ASCII As New System.Text.ASCIIEncoding
If Length > 0 Then
BuffStr = ASCII.GetString(Buffer)
Length = 0
PrevPos = -1
Do
Length = BuffStr.IndexOf(ControlChars.NullChar, PrevPos + 1)
If Length - PrevPos = 1 OrElse Length = -1 Then Exit Do
Try
SectionsINI.Add(BuffStr.Substring(PrevPos + 1, Length - _
PrevPos))
Catch
End Try
PrevPos = Length
Loop
End If
End Function
#End Region
End Module
|