Showing posts with label read file. Show all posts
Showing posts with label read file. Show all posts

Thursday, August 8, 2013

Read file in vb.net


One way

  Dim data As String

        Using sr As New StreamReader("C:\test.txt")
            Do
                data = sr.ReadLine()
                If Not data Is Nothing Then
                    ' do some parsing
                End If
                Console.WriteLine(data)
            Loop Until data Is Nothing
        End Using

2nd way - READ it ALL

Imports System
Imports System.IO

Class ReadALLFile
    Public Shared Sub Main()
        Try 
            Using sr As New StreamReader("TestFile.txt")
                Dim line As String
                line = sr.ReadToEnd()
                Console.WriteLine(line)
            End Using 
        Catch e As Exception         
            Console.WriteLine(e.Message)
        End Try 
    End Sub 
End Class

Read file Character by Character in vb.net


Dim currChar As Char
Dim data As String

  Using sr As New StreamReader("C:\test.txt")
            Do While sr.Peek <> -1
                currChar = Chr(sr.Read)
                data = data & currChar
                Console.WriteLine(data)
            Loop
   End Using