Lister les clés d'une section d'un fichier INI


Contenu du snippet

<DllImport("kernel32.dll", SetLastError:=True, CharSet:=CharSet.Ansi)> _ 
Friend Function GetPrivateProfileSection(ByVal lpAppName As String, ByVal lpReturnedString As IntPtr, ByVal nSize As Integer, ByVal lpFileName As String) As Integer

End Function
Public Function EnumIniKeys(ByVal IniPath As String, ByVal Section As String) As String() 
Dim pBuffer As IntPtr = Marshal.AllocHGlobal(32768)
Dim keys() As String = Nothing


Try
Dim iRet As Integer = GetPrivateProfileSection(Section, pBuffer, 32768, IniPath) 
If (iRet > 0) Then
Dim sReturned As String = Marshal.PtrToStringAnsi(pBuffer, iRet - 1) 
keys = sReturned.Split(vbNullChar.ToCharArray(), StringSplitOptions.RemoveEmptyEntries)

If (keys IsNot Nothing AndAlso keys.Length > 0) Then
For i As Integer = 0 To keys.Length - 1 
keys(i) = keys(i).Substring(0, keys(i).IndexOf("="))
Next


End If

Else

Return Nothing

End If
Catch ex As Exception 
Finally

If pBuffer <> IntPtr.Zero Then

Marshal.FreeCoTaskMem(pBuffer)

End If

End Try
Return keys 
End Function


Compatibilité : VB 2005, VB 2008, VB.NET 1.x

Disponible dans d'autres langages :

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.