La Web del Programador: Comunidad de Programadores
 
    Pregunta:  45742 - MOSTRAR EL CONTENIDO DE UNA ARCHIVO DENTRO DE UN TEXTBOX
Autor:  Angel Pérez
Hola:

Necesito saber como cargo el contenido de un archivo secuencial dentro de un textbox. Lo abro sin problemas y extraigo los datos que deseo en las variables pero no se como hacer que las variables se muestren dentro del textbox hasta que el archivo llegue al EOF.

  Respuesta:  alfredo Juez
he revisado el codigo y efectivamente se debe corregir, aqui os pongo el codigo 100% probado:

Lo primero es añadir al inicio del fichero:

'NECESARIO
Imports System
Imports System.IO
'PARA StreamReader

Luego:

Private Sub LeerTXT()
Dim Archivo As New StreamReader(Application.StartupPath & "\MultiplesPruebas.xml")
Dim Texto As String
Dim Linea As String
Dim i As Integer = 0
Try
Texto = Archivo.ReadLine
Linea = ""
Do While Not Texto Is Nothing
Linea &= Texto & vbCrLf
'vbCrLf -> porque si leemos linea por linea
' hay que añadir luego los saltos de linea
Texto = Archivo.ReadLine
Loop
TextBox.Text = Linea
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Archivo.Close()
Archivo = Nothing
End Sub

Un saludo a tod@s

  Respuesta:  German Lucero
Angel Perez:

No es nada complicado, existen algunas maneras de obtener el resultado que queres. Te muestro la mas conveniente a mi gusto:

Private Sub LeerTXT()
Dim Archivo As New StreamReader(Application.StartupPath & "\archivo.txt")
Dim Texto As String
Dim i As Integer = 0
Try
Texto = Archivo.ReadLine
Do While Not Linea Is Nothing
Texto &= Archivo.ReadLine
Loop
TextBox.Text = Texto
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Archivo.Close()
Archivo = Nothing
End Sub

Te comento que el codigo lo arme recien, creo q no tiene errores, pero el objetivo esta en que te fijes la modalidad.
Espero haberte sacado tus dudas!!!!
Suerte =)

German Lucero