Les Snippets

Connexion

Lister les sections d'un fichier INI

Niveau requis pour utiliser/comprendre cette source : 1 ( Débutant )
Créé le 15/02/2007 14:57:15 et initié par PCPT [Liste]
Date de mise à jour : 17/02/2007 21:47:37
Vue : 6787
Catégorie(s) : Fichier / Disque
Langages dispo pour ce code :
- VB6, VBA
- VB 2005, VB.NET 1.x
- Delphi 5
- Python



Langage : VB6 , VBA
Date ajout : 15/02/2007
Posté par PCPT [Liste]
Private Declare Function GetPrivateProfileSectionNames Lib  "kernel32.dll" Alias "GetPrivateProfileSectionNamesA" (ByVal lpszReturnBuffer As StringByVal nSize As Long, ByVal lpFileName As String) As Long
Private Function EnumSections(ByRef aSections() As String, sIniPath As String) As Boolean
    On Local Error Resume Next
'    on récupère toutes les clés de la section
    Dim sRet As String, sTempSection As String
    sRet = String$(458752, vbNullChar)
    sTempSection = Left$(sRet, GetPrivateProfileSectionNames(sRet, Len(sRet), sIniPath) - 1)
'   en principe c'est pas vide, mais dans le  doute...
    If LenB(sTempSection) = Then
        EnumSections = False
    Else
'       on renvoie toutes les sections
        aSections() = Split(sTempSection, vbNullChar)
        EnumSections = True
    End If
    sRet = vbNullString
    sTempSection = vbNullString
End Function

'   EXEMPLE  D'UTILISATION
Private Sub Form_Load()
    Dim aRet() As String
    If EnumSections(aRet, "C:\BOOT.INI"Then
        Dim As Integer, sRet As String
        sRet = vbNullString
        For i = LBound(aRet) To UBound(aRet)
            sRet = sRet & "[" & aRet(i) & "]" & vbCrLf
        Next i
        MsgBox sRet
    Else
        MsgBox "aucune section"
    End If
    Erase aRet
    Unload Me
End Sub

Langage : VB.NET 1.x , VB 2005
Date ajout : 17/02/2007
Posté par Willi [Liste]
DateMAJ : 17/02/2007
<DllImport("kernel32.dll", CharSet:=CharSet.Auto)> _ 
Function GetPrivateProfileSectionNames( _ 
ByVal lpszReturnBuffer As IntPtr, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
End Function
    Private Function EnumSections(ByRef sections() As String, ByVal path As String) As Boolean
        Dim bRet As Boolean = True
        Dim pBuff As IntPtr = Marshal.AllocCoTaskMem(458752)
        Dim iRet = GetPrivateProfileSectionNames(pBuff, 458752, path)
        If iRet > 0 Then
            Dim sRet As String = Marshal.PtrToStringAuto(pBuff, iRet)
            sections = sRet.Substring(0, sRet.Length - 1).Split(vbNullChar)
        Else
            bRet = False
        End If
        Marshal.FreeCoTaskMem(pBuff)
        Return bRet
    End Function


Remarque :
Ajoutez la directive Imports System.Runtime.InteropServices
Langage : Delphi 5
Date ajout : 22/02/2007
Posté par cirec [Liste]
{Nescessite la déclaration de IniFiles dans les Uses} 
procedure TForm1.Button1Click(Sender: TObject); 
begin   
  With TIniFile.Create('C:\Boot.ini') Do Try     
    ReadSections(Memo1.Lines);   
  Finally     
    Free;   
  End; 
end; 
Langage : Python
Date ajout : 19/03/2007
Posté par pacificator [Liste]
>>> import ConfigParser as cp
>>> cfg = cp.ConfigParser()
>>> fichier = "test.ini"
>>> cfg.read(fichier)
['test.ini']
>>> for section in cfg.sections():
...        print section


Snippets en rapport avec : Fichier, Ini, Lister, Section



Codes sources en rapport avec : Fichier, Ini, Lister, Section

{Visual Basic, VB6, VB.NET, VB 2005} DIRLISTING - LISTER UN DOSSIER ET SES SOUS DOSSIERS TRES RAPIDEMENT
Cette classe vous offre un moyen simple et rapide de lister le contenu d'un repertoire. Elle est in...

{C# / C#.NET} CLASSE FICHIER INI COMPLET
Je vous propose une class qui vous permet de gérer entièrement un fichier INI. Bah oui, je suis touj...

{Visual Basic, VB6, VB.NET, VB 2005} LISTEZ VOS FICHIER FACILEMENT ET RAPIDEMENT
Je pars du même constat que Jack a fait concernant sa source "REMPLACER DIR PAR UNE CLASSE DIR2 (AVA...

{C / C++ / C++.NET} UTILISER L'API INI DE LA LIBCX
Commençons par le début en regardant comment charger le fichier. Puis nous verrons comment récupérer...

{PHP} FONCTION RÉCURSIVE QUI PERMET DE DEPLACER DES FICHIERS CONTENU DANS UN SOUS-REPERTOIRE VERS LE REPERTOIRE PARENTS.
Cette fonction permet de récuperer le contenu de sous répertoire et de les mettre dans le repertoire...

{Visual Basic, VB6, VB.NET, VB 2005} CLASSE DE GESTION DES FICHIERS/DOSSIERS ==> INFOS (DATES, COPYRIGHT...), LISTING, COPIER, CORBEILLE, BOITES DE DIALOGUE ... LE TOUT PAR APIS
Ce code est une classe qui permet de faire différentes actions sur les fichiers et les dossiers, à s...

{C# / C#.NET} PARSEUR DE FICHIER INI
Bon, je sais, les sources qui permettent de lire des fichier ini sont plutôt nombreux et la gestion ...

{Visual Basic, VB6, VB.NET, VB 2005} LISTER CONTENU DISQUE DUR DANS UNE BASE DE DONNÉES ACCESS + RECHERCHE DE FICHIERS DANS CETTE TABLE
Cette source permet en gros : - De lister son disque dur ( donc la fonction pour lister un repert...

{Delphi} TXTMANNAGER
en rodant dans le site je suis tombé sur une source d'un memo qui se rangais dans le systray mais il...

{JAVA / J2EE} GESTION DE FICHIER DE CONFIGURATION .INI
Je devais utiliser un fichier de configuration (.ini) à la windows sur mon appli Java. Du coup, j'ai...