
12/05/2009, 19:45
|
 | | | Fecha de Ingreso: julio-2008
Mensajes: 355
Antigüedad: 16 años, 8 meses Puntos: 2 | |
Respuesta: Leer Archivo Plano linea por linea con vb.net 2008 Cita:
Iniciado por luzstellan Buenos dias
Necesito uqe me colaboren, como hago para leer un archivo plano linea por linea y que la informacion que se lea se capture en una variable en visual para luego poder imprimir la informacion. Usa la función GetPrivateProfileSection
Código:
Código: <Runtime.InteropServices.DllImport("kernel32.dll", SetLastError:=True)> _
Private Shared Function GetPrivateProfileSection(ByVal lpAppName As String, _
ByVal lpReturnedString As IntPtr, _
ByVal nSize As Integer, _
ByVal lpFileName As String) As Short
End Function
Private Sub btnListar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnListar.Click
'creamos la cadena de caracteres
Dim sb As New System.Text.StringBuilder(255)
' asignamos la memoria al puntero
Dim ip As IntPtr = Runtime.InteropServices.Marshal.AllocHGlobal(255)
Try
Dim bt As Byte = GetPrivateProfileSection("Usuarios", ip, 255, "C:\Temp.ini")
For i As Integer = 0 To bt - 1
' convertimos los bytes a caracteres y los añadimos al StringBuilder
sb.Append(Convert.ToChar(Runtime.InteropServices.Marshal.ReadByte(ip, i)))
Next
' eliminamos el último carácter que es nulo
sb.Remove(sb.Length - 1, 1)
' limpiamos el TextBox y añadimos cada línea de la sección [Usuarios]
ListBox1.Items.Clear()
For Each line As String In sb.ToString().Split(Convert.ToChar(0))
ListBox1.Items.Add(line)
Next
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
Finally
' liberamos la memoria
Runtime.InteropServices.Marshal.FreeHGlobal(ip)
End Try
End Sub
|