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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
'Public Class ToutPourFichierINI
Imports System.Runtime.InteropServices
Imports System.IO
Imports System.Text
Public Class ToutPourFichierINI
#Region "Natives Methods"
Private Declare Unicode Function GetPrivateProfileString Lib "kernel32" _
Alias "GetPrivateProfileStringW" (ByVal lpApplicationName As String, _
ByVal lpKeyName As String, ByVal lpDefault As String, _
ByVal lpReturnedString As String, ByVal nSize As Int32, _
ByVal lpFileName As String) As Int32
<DllImport("kernel32")> _
Private Shared Function GetPrivateProfileString(ByVal Section As String, ByVal Key As Integer, ByVal Value As String, <MarshalAs(UnmanagedType.LPArray)> ByVal Result As Byte(), ByVal Size As Integer, ByVal FileName As String) As Integer
End Function
<DllImport("kernel32")> _
Private Shared Function GetPrivateProfileString(ByVal Section As Integer, ByVal Key As String, ByVal Value As String, <MarshalAs(UnmanagedType.LPArray)> ByVal Result As Byte(), ByVal Size As Integer, ByVal FileName As String) As Integer
End Function
Private Declare Unicode Function WritePrivateProfileString Lib "kernel32" _
Alias "WritePrivateProfileStringW" (ByVal lpApplicationName As String, _
ByVal lpKeyName As String, ByVal lpString As String, _
ByVal lpFileName As String) As Int32
#End Region
#Region "Méthodes publiques"
'Suppression de section
Public Shared Sub INIDelete(ByVal INIPath As String, ByVal SectionName As String)
Dim lpKeyName As String = Nothing
Dim lpString As String = Nothing
WritePrivateProfileString(SectionName, lpKeyName, lpString, INIPath)
End Sub
'Suppression de clé
Public Shared Sub INIDelete(ByVal INIPath As String, ByVal SectionName As String, ByVal KeyName As String)
Dim lpString As String = Nothing
WritePrivateProfileString(SectionName, KeyName, lpString, INIPath)
End Sub
'Lecture sections (Clés/Valeurs)
Public Shared Function INIRead(ByVal INIPath As String) As String
Return INIRead(INIPath, Nothing, Nothing, "")
End Function
'Lecture Clés/Valeurs d'une section spécifiée
Public Shared Function INIRead(ByVal INIPath As String, ByVal SectionName As String) As String
Return INIRead(INIPath, SectionName, Nothing, "")
End Function
'Lecture Valeur d'une section et clé spécifiée
Public Shared Function INIRead(ByVal INIPath As String, ByVal SectionName As String, ByVal KeyName As String) As String
Return INIRead(INIPath, SectionName, KeyName, "")
End Function
'Lecture Valeur d'une section et clé spécifiée (retourne vaneur par défaut si elle n'existe pas)
Public Shared Function INIRead(ByVal INIPath As String, ByVal SectionName As String, ByVal KeyName As String, ByVal DefaultValue As String) As String
Dim lpReturnedString As String = Strings.Space(2048)
Dim length As Integer = GetPrivateProfileString(SectionName, KeyName, DefaultValue, lpReturnedString, lpReturnedString.Length, INIPath)
If length > 0 Then
Return lpReturnedString.Substring(0, length)
End If
Return ""
End Function
'Ecriture Clés/Valeurs dans une section spécifiée
Public Shared Sub INIWrite(ByVal INIPath As String, ByVal SectionName As String, ByVal KeyName As String, ByVal TheValue As String)
WritePrivateProfileString(SectionName, KeyName, TheValue, INIPath)
End Sub
'Détecte si section spécifiée existe
Public Shared Function INISectionExist(ByVal INIPath As String, ByVal SectionName As String) As Boolean
For Each sect In INISectionNames(INIPath)
If sect.ToLower = SectionName.ToLower Then
Return True
End If
Next
Return False
End Function
'Retourne tous les noms des sections existantes dans le fichier de configuration
Public Shared Function INISectionNames(ByVal INIPath As String) As String()
Dim maxsize As Integer = 500
While True
Dim bytes As Byte() = New Byte(maxsize - 1) {}
Dim size As Integer = GetPrivateProfileString(0, "", "", bytes, maxsize, INIPath)
If size < maxsize - 2 Then
Dim Selected As String = Encoding.ASCII.GetString(bytes, 0, size - (If(size > 0, 1, 0)))
Return Selected.Split(New Char() {ControlChars.NullChar})
End If
maxsize *= 2
End While
Return Nothing
End Function
'Retourne toutes les clés existantes d'une section spécifiée
Public Shared Function INIEntryNames(ByVal INIPath As String, ByVal section As String) As String()
Dim maxsize As Integer = 500
While True
Dim bytes As Byte() = New Byte(maxsize - 1) {}
Dim size As Integer = GetPrivateProfileString(section, 0, "", bytes, maxsize, INIPath)
If size < maxsize - 2 Then
Dim entries As String = Encoding.ASCII.GetString(bytes, 0, size - (If(size > 0, 1, 0)))
Return entries.Split(New Char() {ControlChars.NullChar})
End If
maxsize *= 2
End While
Return Nothing
End Function
#End Region
End Class |
Partager