Les Snippets

Connexion

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

Niveau requis pour utiliser/comprendre cette source : 1 ( Débutant )
Créé le 12/09/2008 01:30:01 et initié par PCPT [Liste]
Date de mise à jour : 28/09/2008 18:35:37
Vue : 5154
Catégorie(s) : Fichier / Disque
Langages dispo pour ce code :
- VB6, VBA
- VB 2005, VB 2008, VB.NET 1.x
- C# 2.x, C# 3.x
- Delphi 5



Langage : VB6 , VBA
Date ajout : 12/09/2008
Posté par PCPT [Liste]
Private Declare Function GetPrivateProfileSection Lib  "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As StringByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Function EnumIniKeys(ByVal sIniPath As String, ByVal sSection As String, ByRef asKeys() As String) As Long
    Dim sBuffer As String, sKeys As String, i As Long
'
'   on récupère toutes les  clés+valeurs
    sBuffer = VBA.Strings.String$(458752, vbNullChar)
    sKeys = VBA.Strings.Left$(sBuffer, GetPrivateProfileSection(sSection,  sBuffer, Len(sBuffer),  sIniPath) - 1)
    If (VBA.Strings.LenB(sKeys) And VBA.Strings.InStrB(1, sKeys, "=")) Then
        asKeys = VBA.Strings.Split(sKeys,  vbNullChar)
        For i = LBound(asKeys) To UBound(asKeys)
'           l'API nous retourne aussi les valeurs, on  nettoie
            asKeys(i) = VBA.Strings.LeftB$(asKeys(i),  VBA.Strings.InStrB(1,  asKeys(i), "=") - 1)
        Next i
        EnumIniKeys = LBound(asKeys) + UBound(asKeys) + 1
    End If
End Function

Remarque :
ce snippet vient en complément du listage des sections :
http://www.codyx.org/snippet_lister-sections-fichier-ini_306.aspx
Langage : VB.NET 1.x , VB 2005 , VB 2008
Date ajout : 28/09/2008
Posté par Willi [Liste]
<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

Remarque :
Ajouter la directive
Imports System.Runtime.InteropServices

Utilisation:
Dim keys as string()=EnumIniKeys("c:\monIni.ini","Section1")
Langage : C# 2.x , C# 3.x
Date ajout : 28/09/2008
Posté par Willi [Liste]
DateMAJ : 28/09/2008

[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Ansi)]
internal static int GetPrivateProfileSection(string lpAppName, IntPtr lpReturnedString, int nSize, string lpFileName);

public string[] EnumIniKeys(string IniPath, string Section)
{

  IntPtr pBuffer = Marshal.AllocHGlobal(32768);
  string[] keys = null;

  try {

    int iRet = GetPrivateProfileSection(Section, pBuffer, 32768, IniPath);
    if ((iRet > 0))
    {
      string sReturned = Marshal.PtrToStringAnsi(pBuffer, iRet - 1);
      keys = sReturned.Split(char.ConvertFromUtf32(0).ToCharArray(), StringSplitOptions.RemoveEmptyEntries); 
      if ((keys != null && keys.Length > 0))
      {
        for (int i = 0; i <= keys.Length - 1; i++) {
          keys(i) = keys(i).Substring(0, keys(i).IndexOf("="));
        }
      }

    }
    else
    {
      return null;
    }

  }
  catch (Exception ex) {
  }
  finally {
    if (pBuffer != IntPtr.Zero)
    {
      Marshal.FreeCoTaskMem(pBuffer);
    }
  }

  return keys;

}

Remarque :
Ajouter la directive
Using System.Runtime.InteropServices
Langage : Delphi 5
Date ajout : 11/11/2008
Posté par f0xi [Liste]
uses Inifiles;
 
 // List sections
 procedure ListIniSections(const IniFile: string; Sections: TStrings);
  begin
    Sections.BeginUpdate;
    try
      with TIniFile.Create(IniFile) do
      try
        ReadSections(Sections);
      finally
        Free;
      end;
    finally
      Sections.EndUpdate;
    end;
  end;
 
 // list values of section
 procedure ListIniSectionValues(const IniFile, Section: string; Values: TStrings);
 begin
   Values.BeginUpdate;
   try
     with TIniFile.Create(IniFile) do
     try
       ReadSection(Section, Values);
     finally
       Free;
     end;
   finally
     Values.EndUpdate;
   end;
 end;
 
 // list values and datas of section
 procedure ListIniSectionValuesAndDatas(const IniFile, Section: string; ValuesAndDatas: TStrings);
 begin
   ValuesAndDatas.BeginUpdate;
   try
     with TIniFile.Create(IniFile) do
     try
       ReadSectionValues(Section, ValuesAndDatas);
     finally
       Free;
     end;
   finally
     ValuesAndDatas.EndUpdate;
   end;
 end;

Snippets en rapport avec : Fichier, Ini, Lister, Clés, Clefs



Codes sources en rapport avec : Fichier, Ini, Lister, Clés, Clefs

{C / C++ / C++.NET} LISTER LES FICHIERS D'UN REPERTOIRE + FILTRES
Programmé sous Linux. Compatible windows. Liste les fichiers d'un répertoire come indiqué dans le...

{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...