Utilizamos cookies propias y de terceros. [Más información sobre las cookies].
Política de cookies
Proyecto AjpdSoft

· Inicio
· Buscar
· Contactar
· Cookies
· Descargas
· Foros
· Historia
· Nosotros
· Temas
· Top 10
· Trucos
· Tutoriales
· Wiki
Funciones para leer y escribir en ficheros INI VB.Net
Lenguaje de programación Visual Basic .Net

Mostramos varias funciones para leer y escribir en ficheros de configuración INI: leer boolean, string, integer, escribir boolean, string, integer.

Hemos incluido dichas funciones en una clase, de forma que copiando esta clase y agregándola a un proyecto Visual Basic .Net podremos usarlas desde cualquier parte de nuestra aplicación.

Para la lectura y escritura en ficheros INI con VB.Net usamos el API de Windows, las funciones: GetPrivateProfileString, WritePrivateProfileString, GetPrivateProfileInt, FlushPrivateProfileString.


Public Class FicherosINI
    ' Funciones del API de Windows para ficheros INI
    Private Declare Ansi Function GetPrivateProfileString _
      Lib "kernel32.dll" Alias "GetPrivateProfileStringA" _
      (ByVal lpApplicationName As String, _
      ByVal lpKeyName As String, ByVal lpDefault As String, _
      ByVal lpReturnedString As System.Text.StringBuilder, _
      ByVal nSize As Integer, ByVal lpFileName As String) _
      As Integer

    Private Declare Ansi Function WritePrivateProfileString _
      Lib "kernel32.dll" Alias "WritePrivateProfileStringA" _
      (ByVal lpApplicationName As String, _
      ByVal lpKeyName As String, ByVal lpString As String, _
      ByVal lpFileName As String) As Integer

    Private Declare Ansi Function GetPrivateProfileInt _
      Lib "kernel32.dll" Alias "GetPrivateProfileIntA" _
      (ByVal lpApplicationName As String, _
      ByVal lpKeyName As String, ByVal nDefault As Integer, _
      ByVal lpFileName As String) As Integer

    Private Declare Ansi Function FlushPrivateProfileString _
      Lib "kernel32.dll" Alias "WritePrivateProfileStringA" _
      (ByVal lpApplicationName As Integer, _
      ByVal lpKeyName As Integer, ByVal lpString As Integer, _
      ByVal lpFileName As String) As Integer

    Dim strFilename As String

    ' Constructor, para aceptar el fichero INI
    Public Sub New(ByVal Filename As String)
        strFilename = Filename
    End Sub

    'Propiedad sólo lectura con nombre de fichero
    ReadOnly Property FileName() As String
        Get
            Return strFilename
        End Get
    End Property

    'Función para leer cadena de texto (string) de fichero INI
    Public Function GetString(ByVal Section As String, _
      ByVal Key As String, ByVal [Default] As String) As String
        Dim intCharCount As Integer
        Dim objResult As New System.Text.StringBuilder(256)

        intCharCount = GetPrivateProfileString(Section, Key, _
           [Default], objResult, objResult.Capacity, strFilename)
        If intCharCount > 0 Then
            GetString = Left(objResult.ToString, intCharCount)
        Else
            GetString = ""
        End If
    End Function

    'Función para leer un valor numérico del fichero INI
    Public Function GetInteger(ByVal Section As String, _
      ByVal Key As String, ByVal [Default] As Integer) As Integer
        Return GetPrivateProfileInt(Section, Key, _
           [Default], strFilename)
    End Function

    'Función para leer un valor booleano de fichero INI
    Public Function GetBoolean(ByVal Section As String, _
      ByVal Key As String, ByVal [Default] As Boolean) As Boolean
        Return (GetPrivateProfileInt(Section, Key, _
           CInt([Default]), strFilename) = 1)
    End Function

    'Función para escribir valor de cadena (string) en fichero INI
    Public Sub WriteString(ByVal Section As String, _
      ByVal Key As String, ByVal Value As String)
        WritePrivateProfileString(Section, Key, Value, strFilename)
        Flush()
    End Sub

    'Función para escribir valor numérico en fichero INI
    Public Sub WriteInteger(ByVal Section As String, _
      ByVal Key As String, ByVal Value As Integer)
        ' Writes an integer to your INI file
        WriteString(Section, Key, CStr(Value))
        Flush()
    End Sub

    'Función para escribir valor booleano en fichero INI
    Public Sub WriteBoolean(ByVal Section As String, _
    ByVal Key As String, ByVal Value As Boolean)
        ' Writes a boolean to your INI file
        WriteString(Section, Key, CStr(Math.Abs(CInt(Value))))
        Flush()
    End Sub

    'Guarda los cambios de la caché en fichero INI
    Private Sub Flush()
        FlushPrivateProfileString(0, 0, 0, strFilename)
    End Sub
End Class




Publicado el: 2011-04-23

Visita nuestro nuevo sitio web con programas y contenidos actualizados: Proyecto A