Lister une rubrique dans un fichier ini

Résolu
leken Messages postés 33 Date d'inscription vendredi 11 avril 2003 Statut Membre Dernière intervention 16 mars 2012 - 10 juil. 2008 à 00:14
bouv Messages postés 1411 Date d'inscription mercredi 6 août 2003 Statut Membre Dernière intervention 3 mars 2019 - 13 juil. 2008 à 15:43
salut a tous
je debute avec vb net 2008 et je rencontre un problème qui me bloque depuis un petit moment
j'ai un fichier ini de ce genre et je voudrais mettre le contenue de la rubrique [user] dans une liste deroulante


[user]
SXMARCO=Marco|0
SXFRANCK=Franck|0
[Config]
20080703092002484=beta

mon
probleme c'est que je voudrais pouvoir dans une liste deroulante pouvoir mettre
tous les utlisateurs


ex::

ListDeroulante > Marco
                       >Franck

8 réponses

bouv Messages postés 1411 Date d'inscription mercredi 6 août 2003 Statut Membre Dernière intervention 3 mars 2019 1
10 juil. 2008 à 00:37
Voici un module bien utile pour gérer les fichiers ini. Il s'agit de codes que j'ai trouvés ici et là et que j'ai adaptés et regroupés dans un module.
Imports System.Object<?xml:namespace prefix o ns "urn:schemas-microsoft-com:office:office" /??>

Imports System.Runtime.InteropServices.Marshal

 

Module mIni

 

#Region"APIs"

    PrivateDeclareFunction GetPrivateProfileSection Lib"kernel32.dll"Alias"GetPrivateProfileSectionA" (ByVal lpAppName AsString, ByVal lpReturnedBuffer As IntPtr, ByVal nSize AsInteger, ByVal lpFileName AsString) AsInteger

    PrivateDeclareFunction GetPrivateProfileString Lib"kernel32.dll"Alias"GetPrivateProfileStringA" (ByVal lpApplicationName AsString, ByVal lpKeyName AsString, ByVal lpDefault AsString, ByVal lpReturnedBuffer As IntPtr, ByVal nSize AsInteger, ByVal lpFileName AsString) AsInteger

    PrivateDeclareFunction GetPrivateProfileSectionNames Lib"kernel32.dll"Alias"GetPrivateProfileSectionNamesA" (ByVal lpszReturnBuffer() AsByte, ByVal nSize AsInteger, ByVal lpFileName AsString) AsInteger

    PrivateDeclareFunction WritePrivateProfileSection Lib"kernel32.dll"Alias"WritePrivateProfileSectionA" (ByVal lpAppName AsString, ByVal lpString AsString, ByVal lpFileName AsString) AsLong

    PrivateDeclareFunction WritePrivateProfileString Lib"kernel32.dll"Alias"WritePrivateProfileStringA" (ByVal lpApplicationName AsString, ByVal lpKeyName AsString, ByVal lpString AsString, ByVal lpFileName AsString) AsLong

 

    PrivateConst MAX_ENTRY AsInteger = 32768

 

#EndRegion

 

#Region"ENUM"

 

    PublicEnum INIKeyRequest

        KeyName = 0

        KeyValue = 1

    EndEnum

 

#EndRegion

 

#Region"FONCTIONS ET PROCEDURES"

 

    'LIT UNE CLE DANS LE REGISTRE

    PublicFunction INIRead(ByRef Section AsString, ByRef Cle AsString, ByRef FichierIni AsString, OptionalByRef ValeurParDefaut AsString = vbNullString) AsString

        Dim strPtr As IntPtr

        Dim sReturn AsString = vbNullString

        strPtr = StringToHGlobalAnsi(NewString(vbNullChar, 1024))

        sReturn = PtrToStringAnsi(strPtr, GetPrivateProfileString(Section, Cle, "", strPtr, 255, FichierIni))

        FreeHGlobal(strPtr)

        Return sReturn

    EndFunction

 

    'ECRIT UNE CLE DANS LE REGISTRE

    PublicFunction INIWrite(ByRef Section AsString, ByRef Cle AsString, ByRef Valeur AsString, ByRef FichierIni AsString) AsLong

        Return WritePrivateProfileString(Section, Cle, Valeur, FichierIni)

    EndFunction

 

    'EFFACE UNE CLE DANS LE REGISTRE

    PublicFunction INIDelete(ByRef Section AsString, ByRef Cle AsString, ByRef FichierIni AsString) AsLong

        Return WritePrivateProfileString(Section, Cle, Nothing, FichierIni)

    EndFunction

 

    'EFFACE UNE SECTION DU REGISTRE

    PublicFunction INIDeleteSection(ByRef Section AsString, ByRef FichierIni AsString) AsLong

        Return WritePrivateProfileString(Section, Nothing, Nothing, FichierIni)

    EndFunction

 

    'LISTE DES SECTIONS CONTENUES DANS LE FICHIER INI

    PublicFunction INISections(ByRef FichierIni AsString, OptionalByVal sFind AsString = vbNullString) As ArrayList

        Dim MySections = New ArrayList

        Dim Buffer(MAX_ENTRY) AsByte

        Dim BuffStr AsString

        Dim PrevPos AsInteger = 0

        Dim iLength AsInteger

 

        Try

            iLength = GetPrivateProfileSectionNames(Buffer, MAX_ENTRY, FichierIni)

        Catch

            Return MySections

            ExitFunction

        EndTry

 

        Dim ASCII AsNew System.Text.ASCIIEncoding

        If iLength > 0 Then

            BuffStr = ASCII.GetString(Buffer)

            iLength = 0

            PrevPos = -1

            Do

                iLength = BuffStr.IndexOf(ControlChars.NullChar, PrevPos + 1)
                If iLength - PrevPos 1 OrElse iLength -1 Then

                    ExitDo

                EndIf

                Try

                    If sFind = vbNullString Then

                        MySections.Add(BuffStr.Substring(PrevPos + 1, iLength - PrevPos))

                    ElseIf InStr(BuffStr.Substring(PrevPos + 1, iLength - PrevPos), sFind) <> 0 Then

                        MySections.Add(BuffStr.Substring(PrevPos + 1, iLength - PrevPos))

                    EndIf

                Catch

                EndTry

                PrevPos = iLength

            Loop

        EndIf

 

        Return MySections

 

    EndFunction

 

    'LISTE DES CLES CONTENUES DANS UNE SECTION

    PublicFunction INIKeys(ByRef Section AsString, ByVal FichierIni AsString, ByRef ReturnValue As INIKeyRequest, OptionalByVal sFind AsString = vbNullString) As ArrayList

 

        Dim MyKeys = New ArrayList

        Dim sKey AsString = String.Empty

 

        Dim strPtr As IntPtr = StringToHGlobalAnsi(NewString(vbNullChar, 1024))

        Dim iReturn AsInteger = GetPrivateProfileSection(Section, strPtr, 1024, FichierIni)

        Dim sSectionKeys AsString = PtrToStringAnsi(strPtr, iReturn)

        FreeHGlobal(strPtr)

 

        For i AsInteger = 0 To iReturn - 1

            If sSectionKeys.Chars(i) = vbNullChar Then

                'On sépare le nom de la clé de sa valeur

                Dim sKeyName AsString = Left(sKey, InStr(sKey, "=") - 1)

                Dim sKeyValue AsString = Mid(sKey, InStr(sKey, "=") + 1)

                'On prépare le bon renvoi

                Dim sReturn AsString = vbNullString

                SelectCase ReturnValue

                    Case INIKeyRequest.KeyName

                        sReturn = sKeyName

                    Case INIKeyRequest.KeyValue

                        sReturn = sKeyValue

                EndSelect

                'On retourne la clé

                If sFind = vbNullString Then

                    MyKeys.Add(sReturn)

                ElseIf InStr(sKeyName, sFind) <> 0 Then

                    MyKeys.Add(sReturn)

                EndIf

                'On vide la chaine

                sKey = vbNullString

            Else

                sKey = sKey & sSectionKeys.Chars(i)

            EndIf

        Next

 

        Return MyKeys

 

    EndFunction

 

    'TESTE L'EXISTENCE D'UNE SECTION

    PublicFunction INISectionExist(ByRef Section AsString, ByRef FichierIni AsString) AsBoolean

        Dim strPtr As IntPtr

        Dim lReturn AsInteger

        strPtr = StringToHGlobalAnsi(NewString(vbNullChar, 1024))

        lReturn = GetPrivateProfileSection(Section, strPtr, 1024, FichierIni)

        FreeHGlobal(strPtr)

        ReturnCBool(lReturn)

    EndFunction

 

#EndRegion

 

EndModule

 

Pour ton besoin utilise la fonction INIKeys de la façon suivante :
    Dim

MyUsers
As ArrayList = INIKeys("user", "c:\CheminDuFichier.ini", INIKeyRequest.KeyValue
)

    For i AsInteger = 0 To MyUsers.Count - 1

        MaListeDeroulante.Add(MyUsers(i).ToString)

    Next i

Bonne prog
++
3
mortalino Messages postés 6786 Date d'inscription vendredi 16 décembre 2005 Statut Membre Dernière intervention 21 décembre 2011 18
10 juil. 2008 à 08:40
salut,

Bouv :

PrivateDeclareFunction WritePrivateProfileSection Lib"kernel32.dll"Alias"WritePrivateProfileSectionA" (ByVal lpAppName AsString, ByVal lpString AsString, ByVal lpFileName AsString) AsLong

    PrivateDeclareFunction WritePrivateProfileString Lib"kernel32.dll"Alias"WritePrivateProfileStringA" (ByVal lpApplicationName AsString, ByVal lpKeyName AsString, ByVal lpString AsString, ByVal lpFileName AsString) AsLong

ce ne serait pas Integer en dotnet ?

@++

le mystérieux chevalier,"Provençal, le gaulois"
Forum Office & VBA
3
bouv Messages postés 1411 Date d'inscription mercredi 6 août 2003 Statut Membre Dernière intervention 3 mars 2019 1
10 juil. 2008 à 09:55
Salut,

Si effectivement l'Integer est plus indiqué.

Le Long fonctionne également mais il est inutile d'utiliser du 64 bit ici.

Donc : remplacer Long par Integer.

Merci pour l'observation Mortalino.

Bonne prog
++
3
leken Messages postés 33 Date d'inscription vendredi 11 avril 2003 Statut Membre Dernière intervention 16 mars 2012 1
11 juil. 2008 à 00:57
Merci pour votre code il fonctione impec
Pour tous ceux qui utilise VB 2008 juste un petit menage a faire au sein du code mais rien de compliquer
3

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
leken Messages postés 33 Date d'inscription vendredi 11 avril 2003 Statut Membre Dernière intervention 16 mars 2012 1
10 juil. 2008 à 10:07
Juste une chose a dire, merci a tous
je vous tiendrais au courant et merci
0
bouv Messages postés 1411 Date d'inscription mercredi 6 août 2003 Statut Membre Dernière intervention 3 mars 2019 1
11 juil. 2008 à 09:13
Salut,

Je suis en VB.Net 2008, de quel ménage parles tu ?

Bonne prog
++
0
leken Messages postés 33 Date d'inscription vendredi 11 avril 2003 Statut Membre Dernière intervention 16 mars 2012 1
13 juil. 2008 à 15:29
Slt,
quand je parle de menage c'est qu'en utlisant le code ici en copier coler, j'ai eut des erreurs de code
ex : Dim sKeyName AsString  qu'il faut changer par Dim sKeyName As String
Pour ma part il fonctionne tres bien pour le reste.

yep
0
bouv Messages postés 1411 Date d'inscription mercredi 6 août 2003 Statut Membre Dernière intervention 3 mars 2019 1
13 juil. 2008 à 15:43
Ah ok, effectivement c'est bizzare pourtant il s'agit d'un simple copier/coller de mon module

Bonne prog
++
0
Rejoignez-nous