API para manipulação de ficheiros INI
Código da classe
'========================================
' Codificado por Carlos.DF
' fLaSh - 2009-08
' carlosferreiracarlos@hotmail.com
'========================================
Imports System.IO
''' <summary>
''' Classe de suporte aos ficheiros INI por API..
''' </summary>
''' <remarks></remarks>
Partial Public Class INI
'API standard para o suporte INI
Private Declare Unicode Function WritePrivateProfileString Lib "kernel32" _
Alias "WritePrivateProfileStringW" (ByVal lpApplicationName As String, _
ByVal lpKeyName As String, ByVal lpString As String, _
ByVal lpFileName As String) As Int32
Private Declare Unicode Function GetPrivateProfileString Lib "kernel32" _
Alias "GetPrivateProfileStringW" (ByVal lpApplicationName As String, _
ByVal lpKeyName As String, ByVal lpDefault As String, _
ByVal lpReturnedString As String, ByVal nSize As Int32, _
ByVal lpFileName As String) As Int32
Public Shared Function Read(ByVal INIPath As String, _
ByVal SectionName As String, _
ByVal KeyName As String, _
ByVal DefaultValue As String) As String
' Carrega uma string do INI
Dim n As Int32
Dim sData As String
sData = Space$(1024) ' Tamanho maxi da string
n = GetPrivateProfileString(SectionName, KeyName, DefaultValue, _
sData, sData.Length, INIPath)
If n > 0 Then
Return sData.Substring(0, n)
Else
Return ""
End If
End Function
Public Shared Function Read(ByVal INIPath As String, _
ByVal SectionName As String, _
ByVal KeyName As String) As String
' Carrega uma string do INI
Return Read(INIPath, SectionName, KeyName, "")
End Function
Public Shared Function Read(ByVal INIPath As String, _
ByVal SectionName As String) As String
' Carrega uma string do INI
Return Read(INIPath, SectionName, Nothing, "")
End Function
Public Shared Function Read(ByVal INIPath As String) As String
' Carrega uma string do INI
Return Read(INIPath, Nothing, Nothing, "")
End Function
Public Shared Sub Write(ByVal INIPath As String, _
ByVal SectionName As String, _
ByVal KeyName As String, _
ByVal TheValue As String)
' Cria uma nova seccao no INI
Call WritePrivateProfileString(SectionName, KeyName, TheValue, INIPath)
End Sub
Public Shared Sub Delete(ByVal INIPath As String, _
ByVal SectionName As String, _
ByVal KeyName As String)
' Remove uma seccao do INI
Call WritePrivateProfileString(SectionName, KeyName, Nothing, INIPath)
End Sub
Public Shared Sub Delete(ByVal INIPath As String, _
ByVal SectionName As String)
' Remove uma seccao do INI
Call WritePrivateProfileString(SectionName, Nothing, Nothing, INIPath)
End Sub
End Class
Exemplo de utilização
' Para escrever
INI.Write("INI_PATH", "seccao", "key", "qualquer_valor")
'Para ler
Dim qualquer_variavel As String = INI.Read("INI_PATH", "seccao", "key", "quaquer_valor_por_defeito")
Limitações conhecidas
Os valores a guardar/ler não podem ter mais de 1024 caracteres.