COMO CREAR UN ARCHIVO.INI Y LEER SU CONTENIDO DESDE VB6
HOLA AMIGOS MI PREGUNTA ES LA SIGUIENTE;
COMO PUEDO CREAR UN ARCHIVO .INI Y COMO PUEDO LEER SU CONTENIDO DESDE UNA APLICACION EN VB6.
ANTE CUALQUIER RESPUESTA MUCHAS GRACIAS!!
COMO PUEDO CREAR UN ARCHIVO .INI Y COMO PUEDO LEER SU CONTENIDO DESDE UNA APLICACION EN VB6.
ANTE CUALQUIER RESPUESTA MUCHAS GRACIAS!!
Holas!! Bueno, para escribir y leer un fichero INI yo hago esto:
FUNCION DE LECTURA
Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" _
(ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, _
ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As _
String) As Long
FUNCION DE ESCRITURA
Declare Function WritePrivateProfileString Lib "kernel32" Alias _
"WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As _
Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
PARA LEER
Private Sub Form_Load()
' Leer de "Ejemplo.ini"
Dim I As Integer
Dim x As Integer
Dim Est As String
Est = String$(50, " ")
I = GetPrivateProfileString("Ejemplo", "Nombre", "", Est, Len(Est), "Ejemplo.ini")
If I > 0 Then
MsgBox "Tu Nombre es: " & Est
End If
Est = String$(50, " ")
x = GetPrivateProfileString("Ejemplo", "Datos", "", Est, Len(Est), "Ejemplo.ini")
If x > 0 Then
MsgBox "Tus Datos son: " & Est
End If
End Sub
PARA ESCRIBIR:
Private Sub Form_Unload(Cancel As Integer)
' Escribir en "Ejemplo.ini"
Dim I As Integer
Dim Est As String
Est = "Ejemplo - Apartado"
I = WritePrivateProfileString("Ejemplo", "Nombre", Est, "Ejemplo.ini")
End Sub
(CONTENIDO DEL FICHERO INI)
[Ejemplo]
Nombre=Ejemplo - Apartado
Bueno, si tienes alguna duda... ya sabes, saludos!
FUNCION DE LECTURA
Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" _
(ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, _
ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As _
String) As Long
FUNCION DE ESCRITURA
Declare Function WritePrivateProfileString Lib "kernel32" Alias _
"WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As _
Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
PARA LEER
Private Sub Form_Load()
' Leer de "Ejemplo.ini"
Dim I As Integer
Dim x As Integer
Dim Est As String
Est = String$(50, " ")
I = GetPrivateProfileString("Ejemplo", "Nombre", "", Est, Len(Est), "Ejemplo.ini")
If I > 0 Then
MsgBox "Tu Nombre es: " & Est
End If
Est = String$(50, " ")
x = GetPrivateProfileString("Ejemplo", "Datos", "", Est, Len(Est), "Ejemplo.ini")
If x > 0 Then
MsgBox "Tus Datos son: " & Est
End If
End Sub
PARA ESCRIBIR:
Private Sub Form_Unload(Cancel As Integer)
' Escribir en "Ejemplo.ini"
Dim I As Integer
Dim Est As String
Est = "Ejemplo - Apartado"
I = WritePrivateProfileString("Ejemplo", "Nombre", Est, "Ejemplo.ini")
End Sub
(CONTENIDO DEL FICHERO INI)
[Ejemplo]
Nombre=Ejemplo - Apartado
Bueno, si tienes alguna duda... ya sabes, saludos!
Jolin, q mal ando esta mañana.. para leer más de un dato lo único q tienes q hacer es ir poniendo en las funcion de lectura.. a ver, por ejemplo, un fichero INI que tenga más de un campo:
FICHERO INI
[Carroc]
SQLServidor=ERIKA
SQLDatos=Biyona_
Logo=C:ErikaClaseVisual BasicBIYONALogo.gif
TipoIVA=16,00
Impuestos=SI
PARA LEER
Private Sub Conexion()
On Error GoTo ErrorConexion
Dim i As Integer
Dim X As Integer
Dim Y As Integer
Dim Z As Integer
Dim T As Integer
Dim Impu As String
Dim TipoIVA As String
Dim Servidor As String
Dim BaseDatos As String
Dim ConexionDatos As String
Cerrar = False
If Dir("C:WINDOWSCarroc.ini", vbArchive) = "" Then
MsgBox "Ha ocurrido un error interno. La aplicación no se pudo iniciar ", vbCritical, "Error"
Cerrar = True
Else
Servidor = String$(150, " ")
i = GetPrivateProfileString("Carroc", "SQLServidor", "", Servidor, Len(Servidor), "Carroc.ini")
BaseDatos = String$(150, " ")
X = GetPrivateProfileString("Carroc", "SQLDatos", "", BaseDatos, Len(BaseDatos), "Carroc.ini")
Logos = String$(150, " ")
Y = GetPrivateProfileString("Carroc", "Logo", "", Logos, Len(Logos), "Carroc.ini")
TipoIVA = String$(50, " ")
Z = GetPrivateProfileString("Carroc", "TipoIVA", "", TipoIVA, Len(TipoIVA), "Carroc.ini")
Impu = String$(50, " ")
Z = GetPrivateProfileString("Carroc", "Impuestos", "", Impu, Len(Impu), "Carroc.ini")
TipoDeIva = TipoIVA
IVA = CDbl(TipoDeIva)
IVA = Format(IVA, "##0.#0")
TipoDeImpuesto = Impu
If TipoDeImpuesto = "SI" Then
Impuestos = True
Else
Impuestos = False
End If
Server = Servidor
BaseDeDatos = BaseDatos
If Y > 0 Then
If Not Dir(Logos, vbArchive) = "" Then
Imagen.Picture = LoadPicture(Logos)
End If
End If
ConexionDatos = "Initial Catalog=" & BaseDeDatos & ";Data Source=" & Server
cn.Open ("Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;" & ConexionDatos)
End If
If Cerrar = True Then
Unload Me
End If
ErrorConexion:
MsgBox Err.Number
If Err.Number = -2147467259 Then
MsgBox "La base de datos o el servidor de SQL Server no existe. No se pudo iniciar la aplicación. ", vbCritical
Unload Me
End If
End Sub
FICHERO INI
[Carroc]
SQLServidor=ERIKA
SQLDatos=Biyona_
Logo=C:ErikaClaseVisual BasicBIYONALogo.gif
TipoIVA=16,00
Impuestos=SI
PARA LEER
Private Sub Conexion()
On Error GoTo ErrorConexion
Dim i As Integer
Dim X As Integer
Dim Y As Integer
Dim Z As Integer
Dim T As Integer
Dim Impu As String
Dim TipoIVA As String
Dim Servidor As String
Dim BaseDatos As String
Dim ConexionDatos As String
Cerrar = False
If Dir("C:WINDOWSCarroc.ini", vbArchive) = "" Then
MsgBox "Ha ocurrido un error interno. La aplicación no se pudo iniciar ", vbCritical, "Error"
Cerrar = True
Else
Servidor = String$(150, " ")
i = GetPrivateProfileString("Carroc", "SQLServidor", "", Servidor, Len(Servidor), "Carroc.ini")
BaseDatos = String$(150, " ")
X = GetPrivateProfileString("Carroc", "SQLDatos", "", BaseDatos, Len(BaseDatos), "Carroc.ini")
Logos = String$(150, " ")
Y = GetPrivateProfileString("Carroc", "Logo", "", Logos, Len(Logos), "Carroc.ini")
TipoIVA = String$(50, " ")
Z = GetPrivateProfileString("Carroc", "TipoIVA", "", TipoIVA, Len(TipoIVA), "Carroc.ini")
Impu = String$(50, " ")
Z = GetPrivateProfileString("Carroc", "Impuestos", "", Impu, Len(Impu), "Carroc.ini")
TipoDeIva = TipoIVA
IVA = CDbl(TipoDeIva)
IVA = Format(IVA, "##0.#0")
TipoDeImpuesto = Impu
If TipoDeImpuesto = "SI" Then
Impuestos = True
Else
Impuestos = False
End If
Server = Servidor
BaseDeDatos = BaseDatos
If Y > 0 Then
If Not Dir(Logos, vbArchive) = "" Then
Imagen.Picture = LoadPicture(Logos)
End If
End If
ConexionDatos = "Initial Catalog=" & BaseDeDatos & ";Data Source=" & Server
cn.Open ("Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;" & ConexionDatos)
End If
If Cerrar = True Then
Unload Me
End If
ErrorConexion:
MsgBox Err.Number
If Err.Number = -2147467259 Then
MsgBox "La base de datos o el servidor de SQL Server no existe. No se pudo iniciar la aplicación. ", vbCritical
Unload Me
End If
End Sub