quarta-feira, 18 de outubro de 2017

Ler arquivos TXT - VB.NET

Neste artigo vou mostrar como ler um arquivo .txt;

Primeiro importe o System.IO


1:      Dim FILE_NAME As String = "c:\teste.txt"  
2:      Dim TextLine As String  
3:      If System.IO.File.Exists(FILE_NAME) = True Then  
4:        Dim objReader As New System.IO.StreamReader(FILE_NAME)  
5:        Do While objReader.Peek() <> -1  
6:          TextLine = TextLine & objReader.ReadLine() & vbNewLine  
7:        Loop  
8:        TextBox1.Text = TextLine  
9:         
10:      Else  
11:        MsgBox("Este arquivo não existe")  
12:      End If  

Dessa forma, lemos um arquivo .txt e jogamos em uma TextBox.

Aqui especificamos o caminho onde nossa .txt está, você também pode usar um OpenFileDialog para procurar o arquivo

Dim FILE_NAME As String = "c:\teste.txt"

Aqui verificamos se o arquivo existe

If System.IO.File.Exists(FILE_NAME) = True Then


Aqui fazemos a leitura e jogamos na string TextLine que foi criada localmente

Do While objReader.Peek() <> -1  
  TextLine = TextLine & objReader.ReadLine() & vbNewLine  
Loop  

Aqui jogamos a string textline na nossa textbox
TextBox1.Text = TextLine  


E pronto, temos uma leitura de um arquivo .txt

COMENTE

& Compartilhe

0 comentários:

Postar um comentário

 

Copyright © 2015 Projeto Programação

Distributed By My Blogger Themes | Designed by Templateism