D'accord avec PCPT sur le standard INI. Cependant PCPT, on peut utiliser
les API pour se simplifier le travail de base.
Si tu es obligé de faire avec le problème de clefs multiples dans une section,
voilà peut être une classe (adaptée de la source de Donf indiquée
dans mon premier message) qui peut te donner un début de solution.
Sinon, il serai plus efficace de restructure ton fichier INI.
Imports System.Collections.Generic
Public Class BizarreINI
Private Declare Function GetPrivateProfileSection Lib "kernel32.dll" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedBuffer As String, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
Private Declare Function GetPrivateProfileSectionNames Lib "kernel32.dll" Alias "GetPrivateProfileSectionNamesA" (ByVal lpszReturnBuffer As String, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
Private Declare Function GetPrivateProfileString Lib "kernel32.dll" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedBuffer As String, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
Public Sub New()
End Sub
Public Sub New(ByVal INIFileName As String)
Me.FileName = INIFileName
End Sub
Private pFileName As String = Nothing
Public Property FileName() As String
Get
Return Me.pFileName
End Get
Set(ByVal value As String)
If Me.pFileName = value Then Exit Property
If Not System.IO.File.Exists(value) Then value = Nothing
Me.pFileName = value
End Set
End Property
'------------------------------------------------------------------------------------------------
'-- Retourne un booléen indiquant l'existance ou non d'une section
'------------------------------------------------------------------------------------------------
Public Function ExisteSection(ByVal Section As String) As Boolean
If Me.pFileName = Nothing Then Return False
Dim ReturnBuffer As New String(CChar(vbNullChar), 1024)
Return GetPrivateProfileSection(Section, ReturnBuffer, ReturnBuffer.Length, Me.pFileName) <> 0
End Function
'------------------------------------------------------------------------------------------------
'-- Retourne un objet List(Of String) contenant l'ensemble
'-- des sections du fichier INI associé à la propriété FileName
'--
'-- Utilisation :
'-- Dim instance As New BizarreINI(CeminCompletDuFichierIni)
'-- Dim Sections As System.Collections.Generic.List(Of String) = instance.GetAllSections()
'--
'-- La fonction retourne Nothing en cas d'échec sinon la lecture des données se fait par :
'-- Sections(0)
'-- Sections(1)
'-- ...
'-- Sections(n)
'------------------------------------------------------------------------------------------------
Public Function GetAllSections() As List(Of String)
If Me.pFileName = Nothing Then Return Nothing
Dim ReturnBuffer As New String(CChar(vbNullChar), 1024)
GetPrivateProfileSectionNames(ReturnBuffer, ReturnBuffer.Length, Me.pFileName)
Dim ArrayOfSections As String() = ReturnBuffer.Split(New Char() {CChar(vbNullChar)}, System.StringSplitOptions.RemoveEmptyEntries)
Dim SectionsList As New List(Of String)
For Each Item As String In ArrayOfSections
SectionsList.Add(Item)
Next Item
Return SectionsList
End Function
'------------------------------------------------------------------------------------------------
'-- Retourne un objet List(Of String) à deux dimension contenant l'ensemble des clefs
'-- et de leurs valeurs d'une section du fichier INI associé à la propriété FileName
'--
'-- Utilisation :
'-- Dim instance As New BizarreINI(CeminCompletDuFichierIni)
'-- Dim Clefs As System.Collections.Generic.List(Of String()) = Me.IniFile.GetSectionCles(NomDeLaSection)
'--
'-- La fonction retourne Nothing en cas d'échec sinon la lecture des données se fait par :
'-- Clefs(0)(0) : nom de la clef / Clefs(0)(1) : valeur de la clef
'-- Clefs(1)(0) / Clefs(1)(1)
'-- ...
'-- Clefs(n)(0) / Clefs(n)(1)
'------------------------------------------------------------------------------------------------
Public Function GetSectionCles(ByVal Section As String) As List(Of String())
If Me.pFileName = Nothing Then Return Nothing
Dim ReturnBuffer As New String(CChar(vbNullChar), 1024)
GetPrivateProfileSection(Section, ReturnBuffer, ReturnBuffer.Length, Me.pFileName)
Dim ArrayOfKeys As String() = ReturnBuffer.Split(New Char() {CChar(vbNullChar)}, System.StringSplitOptions.RemoveEmptyEntries)
Dim ArrayOfKeysList As New List(Of String())
Dim SeparatorIndex As Integer = Nothing
For Each Item As String In ArrayOfKeys
SeparatorIndex = Item.IndexOf("=")
ArrayOfKeysList.Add(New String() {Item.Substring(0, SeparatorIndex), Item.Substring(SeparatorIndex + 1)})
Next Item
Return ArrayOfKeysList
End Function
'------------------------------------------------------------------------------------------------
'-- Retourne un objet List(Of String) à deux dimension contenant l'ensemble des clefs
'-- spécifiées par le paramêtre KeyName et de leurs valeurs d'une section du fichier
'-- INI associé à la propriété FileName
'--
'-- Utilisation :
'-- Dim instance As New BizarreINI(CeminCompletDuFichierIni)
'-- Dim Clefs As System.Collections.Generic.List(Of String()) = Me.IniFile.GetSectionCles(NomDeLaSection, NomDeLaClef)
'--
'-- La fonction retourne Nothing en cas d'échec sinon la lecture des données se fait par :
'-- Clefs(0)(0) : nom de la clef / Clefs(0)(1) : valeur de la clef
'-- Clefs(1)(0) / Clefs(1)(1)
'-- ...
'-- Clefs(n)(0) / Clefs(n)(1)
'------------------------------------------------------------------------------------------------
Public Function GetSectionCles(ByVal Section As String, ByVal KeyName As String) As List(Of String())
If Me.pFileName = Nothing Then Return Nothing
Dim ArrayOfKeysList As List(Of String()) = Me.GetSectionCles(Section)
Dim ArrayOfNamedKeysList As New System.Collections.Generic.List(Of String())
For Each Item As String() In ArrayOfKeysList
If Item(0) = KeyName then ArrayOfNamedKeysList.Add(New String() {Item(0), Item(1)})
Next Item
Return ArrayOfNamedKeysList
End Function
'------------------------------------------------------------------------------------------------
'-- Retourne un objet String contenant la valeur d'une clef spécifiée par le paramêtre KeyName
'-- et de rang spécifié par le paramêtre Index d'une section du fichier INI associé
'-- à la propriété FileName.
'-- Permet d'obtenir une clef particulière en cas de multiplicité du nom de la clef.
'--
'-- Utilisation :
'-- Dim instance As New BizarreINI(CeminCompletDuFichierIni)
'-- Dim valeur As String = Me.IniFile.GetCle(NomDeLaSection, NomDeLaClef, IndexDeLaClef)
'--
'-- La fonction retourne Nothing en cas d'échec sinon la valeur de la clef
'------------------------------------------------------------------------------------------------
Public Function GetCle(ByVal Section As String, ByVal Clef As String, ByVal Index As Integer) As String
If Me.pFileName = Nothing Then Return Nothing
Dim ArrayOfNamedKeysList As List(Of String()) = Me.GetSectionCles(Section, Clef)
Dim ReturnValue As String = Nothing
Try
ReturnValue = ArrayOfNamedKeysList(Index)(1)
Catch ex As Exception
End Try
Return ReturnValue
End Function
'------------------------------------------------------------------------------------------------
'-- Retourne un objet String contenant la valeur de la première clef spécifiée par le paramêtre
'-- KeyName d'une section du fichier INI associé à la propriété FileName.
'-- Permet d'obtenir une clef unique ou la première pour une clef multiple.
'--
'-- Utilisation :
'-- Dim instance As New BizarreINI(CeminCompletDuFichierIni)
'-- Dim valeur As String = Me.IniFile.GetCle(NomDeLaSection, NomDeLaClef)
'--
'-- La fonction retourne Nothing en cas d'échec sinon la valeur de la clef
'------------------------------------------------------------------------------------------------
Public Function GetCle(ByVal Section As String, ByVal Clef As String) As String
If Me.pFileName = Nothing Then Return Nothing
Return GetCle(Section, Clef, 0)
End Function
End Class
A+