xanburzum 169 Denunciar post Postado Novembro 16, 2008 O System.IO.StreamReader é fácil de usar ,através looping de um arquivo de texto e exibir o conteúdo de uma página. Este exemplo de código lê um arquivo chamado lista.txt e exibe a saída para um controle Label. Também é criada uma variável 'x' que mantém registro de quantas linhas estão no arquivo de texto e exibe os resultados para outro controle Label. <%@ Page Language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> Sub Page_Load() Dim reader As System.IO.StreamReader Dim sb As New System.Text.StringBuilder Dim line As String = Nothing Dim x As Integer = 0 Try 'Abrir um arq. de entrada e ler linha por linha 'Isso mostra como utilizar Server.MapPath para mapear o caminho físico do arquivo reader = New System.IO.StreamReader(Server.MapPath(".") & "/ista.txt") sb.Append("<br>") 'Loop através do objeto Stream Do While reader.Peek() >= 0 line = reader.ReadLine() sb.Append(line & "<BR>") x = x + 1 Loop 'Mostrar o número de linhas no arquivo de texto para o controle Label1 Label1.Text = x.ToString() 'Escrever o texto para o controle Label2 Label2.Text = sb.ToString() Catch ex As Exception Response.Write(ex.ToString()) End Try End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Ler dados de um arquivo texto utilizando System.IO.StreamReader</title> </head> <body> <form id="form1" runat="server"> <div> Numero de linhas: <b><asp:Label ID=Label1 runat=server></asp:Label></b> <br /> <br /> <b>Saída a partir do arquivo de texto.</b> <asp:Label ID=Label2 runat=server></asp:Label> </div> </form> </body> </html> Compartilhar este post Link para o post Compartilhar em outros sites