1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
#Region "Lire INI"
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (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
' Cette API permet de lire des fichiers d'initialisations. La fonction retourne 0 si la lecture du fichier d'initialisation a échoué, sinon elle retourne 1.
'lpApplicationName définit le nom de la section,
'lpKeyName définit le nom de la clef,
'lpDefault retourne la valeur par défaut si la clef n'est pas définie,
'lpReturnedString contient la valeur de la clef si elle existe sinon elle contient la valeur lpDefault,
'nSize est la taille de la variable lpReturnedString en octet,
'lpFileName définit le nom du fichier d'initialisation.
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 "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
' Cette API permet de créer ou de modifier vos propres fichiers d'initialisations sans se soucier des détails (par exemple... la gestion des fichiers), si le fichier n'existe pas il est créé, sinon il est modifié. La fonction retourne 0 si la création ou la modification du fichier d'initialisation a échoué, sinon elle retourne 1.
'lpApplicationName définit le nom de la section,
'lpKeyName définit le nom de la clef,
'lpString définit la valeur à ajouter,
'lpFileName définit le nom du fichier d'initialisation.
Public Sub EcrireINI(ByVal Entete As String, ByVal Variable As String, ByVal Valeur As String)
WritePrivateProfileString(Entete, Variable, Valeur, Fichier)
End Sub
#End Region
#Region "Supprime INI"
Public Sub SupprimeINI(ByVal Entete As String, Optional ByVal Variable As String = Nothing)
WritePrivateProfileString(Entete, Variable, vbNullString, Fichier)
End Sub
#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 |
Partager