Advertisement
3DotDev

Classe de gestion de fichier .ini (P/Invoke)

Aug 16th, 2013
696
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 5.09 KB | None | 0 0
  1. '########################################################
  2. ' Credits 3DotDev from http://3dotdevcoder.blogspot.fr/
  3. '########################################################
  4. Imports System.Runtime.InteropServices
  5. Imports System.IO
  6. Imports System.Text
  7.  
  8. Public Class Ini
  9.  
  10. #Region "Natives Methods"
  11.  
  12.     Private Declare Unicode Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringW" (lpApplicationName As String,
  13.                                                                                                                lpKeyName As String, lpDefault As String,
  14.                                                                                                                lpReturnedString As String, nSize As Integer,
  15.                                                                                                                lpFileName As String) As Integer
  16.  
  17.     Private Declare Unicode Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringW" (lpApplicationName As String,
  18.                                                                                                                lpKeyName As String, lpString As String,
  19.                                                                                                                lpFileName As String) As Integer
  20.  
  21.     <DllImport("kernel32")>
  22.     Private Shared Function GetPrivateProfileString(Section As String, Key As Integer, Value As String, <MarshalAs(UnmanagedType.LPArray)> Result As Byte(), Size As Integer, FileName As String) As Integer
  23.     End Function
  24.  
  25.     <DllImport("kernel32")>
  26.     Private Shared Function GetPrivateProfileString(Section As Integer, Key As String, Value As String, <MarshalAs(UnmanagedType.LPArray)> Result As Byte(), Size As Integer, FileName As String) As Integer
  27.     End Function
  28.  
  29. #End Region
  30.  
  31. #Region "Public Methods"
  32.  
  33.     'Suppression de section
  34.     Public Shared Sub DeleteSection(INIPath As String, SectionName As String)
  35.         Dim lpKeyName As String = Nothing
  36.         Dim lpString As String = Nothing
  37.         WritePrivateProfileString(SectionName, lpKeyName, lpString, INIPath)
  38.     End Sub
  39.  
  40.     'Suppression de clé
  41.     Public Shared Sub DeleteKey(INIPath As String, SectionName As String, KeyName As String)
  42.         Dim lpString As String = Nothing
  43.         WritePrivateProfileString(SectionName, KeyName, lpString, INIPath)
  44.     End Sub
  45.  
  46.     'Lecture Valeur d'une section et clé spécifiée
  47.     Public Shared Function ReadValue(INIPath As String, SectionName As String, KeyName As String) As String
  48.         Return ReadValue(INIPath, SectionName, KeyName, "")
  49.     End Function
  50.  
  51.     'Lecture Valeur d'une section et clé spécifiée (retourne valeur par défaut si elle n'existe pas)
  52.     Public Shared Function ReadValue(INIPath As String, SectionName As String, KeyName As String, DefaultValue As String) As String
  53.         Dim lpReturnedString As String = Strings.Space(2048)
  54.         Dim length As Integer = GetPrivateProfileString(SectionName, KeyName, DefaultValue, lpReturnedString, lpReturnedString.Length, INIPath)
  55.         If length > 0 Then
  56.             Return lpReturnedString.Substring(0, length)
  57.         End If
  58.         Return ""
  59.     End Function
  60.  
  61.     'Ecriture Clés/Valeurs dans une section spécifiée
  62.     Public Shared Sub Write(INIPath As String, SectionName As String, KeyName As String, TheValue As String)
  63.         WritePrivateProfileString(SectionName, KeyName, TheValue, INIPath)
  64.     End Sub
  65.  
  66.     'Détecte si section spécifiée existe
  67.     Public Shared Function SectionExists(INIPath As String, SectionName As String) As Boolean
  68.         Return SectionNames(INIPath).Any(Function(s) s.ToLower = SectionName.ToLower)
  69.     End Function
  70.  
  71.     'Retourne tous les noms des sections existantes dans le fichier de configuration
  72.     Public Shared Function SectionNames(INIPath As String) As String()
  73.         Dim maxsize As Integer = 500
  74.         While True
  75.             Dim bytes As Byte() = New Byte(maxsize - 1) {}
  76.             Dim size As Integer = GetPrivateProfileString(0, "", "", bytes, maxsize, INIPath)
  77.             If size < maxsize - 2 Then
  78.                 Dim Selected As String = Encoding.ASCII.GetString(bytes, 0, size - (If(size > 0, 1, 0)))
  79.                 Return Selected.Split(New Char() {ControlChars.NullChar})
  80.             End If
  81.             maxsize *= 2
  82.         End While
  83.         Return Nothing
  84.     End Function
  85.  
  86.     'Retourne toutes les clés existantes d'une section spécifiée
  87.     Public Shared Function SectionKeys(INIPath As String, sectionName As String) As String()
  88.         Dim maxsize As Integer = 500
  89.         While True
  90.             Dim bytes As Byte() = New Byte(maxsize - 1) {}
  91.             Dim size As Integer = GetPrivateProfileString(sectionName, 0, "", bytes, maxsize, INIPath)
  92.             If size < maxsize - 2 Then
  93.                 Dim entries As String = Encoding.ASCII.GetString(bytes, 0, size - (If(size > 0, 1, 0)))
  94.                 Return entries.Split(New Char() {ControlChars.NullChar})
  95.             End If
  96.             maxsize *= 2
  97.         End While
  98.         Return Nothing
  99.     End Function
  100.  
  101. #End Region
  102.  
  103. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement