0 calificaciones0% encontró este documento útil (0 votos)
59 vistas2 páginas
Se ha incluido una clase que permite leer y escribir datos en ficheros INI de Windows mediante funciones del API como GetPrivateProfileString y WritePrivateProfileString. La clase incluye métodos para obtener y establecer cadenas de texto, enteros, y valores booleanos en el fichero INI especificado.
Se ha incluido una clase que permite leer y escribir datos en ficheros INI de Windows mediante funciones del API como GetPrivateProfileString y WritePrivateProfileString. La clase incluye métodos para obtener y establecer cadenas de texto, enteros, y valores booleanos en el fichero INI especificado.
Se ha incluido una clase que permite leer y escribir datos en ficheros INI de Windows mediante funciones del API como GetPrivateProfileString y WritePrivateProfileString. La clase incluye métodos para obtener y establecer cadenas de texto, enteros, y valores booleanos en el fichero INI especificado.
Hemos incluido dichas funciones en una clase, de forma que copiando
esta clase y agregndola a un proyecto Visual Basic .Net podremos
usarlas desde cualquier parte de nuestra aplicacin. 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 slo lectura con nombre de fichero ReadOnly Property FileName() As String Get Return strFilename End Get End Property 'Funcin 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 'Funcin para leer un valor numrico 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 'Funcin 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 'Funcin 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 'Funcin para escribir valor numrico 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 'Funcin 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