logo VB.Net - Read and Write Text File
Guide Contents
DigitalDan Home Page

Read a Text File one line at a a time
There are two approaches, the first opens the file with "Dim" and the second involves "Using" You can use either of these approaches in most applications. These examples involve the "Using" approach.
Readers who prefer the "Dim" approach can convert the examples as follows...
Replace the first "Using" with "Dim"
Replace the "End Using" with "sr1.Close: sr1.Dispose"
 
Private Shared Function ReadFile(fileName as string) as List(Of String)
Dim ret As New List(of String): ret.Clear
Dim linein As String
Dim errorMessage As String
Try
Using sr1 As New IO.StreamReader(fileName)
While Not sr1.EndOfStream
linein = sr1.ReadLine
' Processes line of text held in linein
ret.Add(linein)
End While
End Using
Catch ex as Exception
errorMessage = Ex.Message
End Try
Return ret
End Function

 
Read a Text File - Entire file in one go
This example reads the entire file as one long string. The next example demonstrates reading the entire file into an arreay of lines.
 
Private Shared Function ReadFile(fileName as string) as String
Dim allIn As String = String.Empty
Dim errorMessage As String
Try Using sr1 As New IO.StreamReader(fileName)
allIn = sr1.ReadToEnd() End Using Catch ex as Exception
errorMessage = Ex.Message
End Try
Return allIn
End Function

 
Read entire text file as an array of text file lines
 
Private Shared Function ReadFile(fileName as string) as String()
Dim ret As String() = Array.Empty(Of String)
Dim errorMessage As String
Try
Using sr1 As New IO.StreamReader(fileName)
ret = sr1.ReadToEnd().Split(vbcrlf)
End Using
Catch ex as Exception
errorMessage = Ex.Message
End Try
Return ret
End Function

 

Notes
Take care when loading large files - program could break when a large file loaded into memory
Reading entire file at once then trying to split it into lines may not work as expected
Although most text files end each line with "vbcrlf" {chr(13) & chr(10)} you can encounter files ending vbcr {chr(13)} or vblf{chr(10)}

Write a Text File one line at a a time
There are two approaches, the first opens the file with "Dim" and the second involves "Using" You can use either of these approaches in most applications. These examples involve the "Using" approach.
Readers who prefer the "Dim" approach can convert the examples as follows...
Replace the first "Using" with "Dim"
Replace the "End Using" with "sw1.Close: sw1.Dispose"
 
There are 3 versions of the line "Using sw1 As New IO.StreamWriter(fileName)"
Using sw1 As New IO.StreamWriter(fileName) = if file exists then throw an error message
Using sw1 As New IO.StreamWriter(fileName, False) = if file exists then replace it with a new file
Using sw1 As New IO.StreamWriter(fileName, True) = if file exists then add lines to end of file

 
Private Shared Function WriteFile(fileName As String, lines() As String) As String
Dim errorMessage As String = String.Empty
Try
Using sw1 As New IO.StreamWriter(fileName)
For Each s As String In lines
sw1.WriteLine(s)
Next
End Using
Catch ex As Exception errorMessage = ex.Message
End Try
Return errorMessage
End Function

 
Write a Text File - Avoiding final linefeed
The example above will create a blank/empty line at the end of the file because .WriteLine always adds a vbCrLf end-of-line chatacter. To cure the problem, ensure you use .Write instead of .WriteLine when writing the last line of text. The following code avoids the stray line-feed.
 
Private Shared Function WriteFile(fileName As String, lines() As String) As String
Dim errorMessage As String = String.Empty
Try
Using sw1 As New IO.StreamWriter(fileName)
For i As Integer = 0 To lines.Length - 1
If i < lines.Length - 1 Then
sw1.WriteLine(lines(i))
Else
sw1.WriteLine(lines(i))
End If
Next
End Using
Catch ex As Exception
errorMessage = ex.Message
End Try
Return errorMessage
End Function

 
Write a Text File - Entire file in one go
This example writes the entire contents of a string array in one go

 
Private Shared Function WriteFile(filename As String, lines() As String) As String
Dim errorMessage As String = String.Empty
Try
Using sw1 As New IO.StreamWriter(filename)
sw1.Write(String.Join(vbCrLf, lines))
End Using
Catch ex As Exception
errorMessage = ex.Message
End Try
Return errorMessage
End Function

 
Notes
When wtiting text files, it is important to understand the difference between sw1.Write and sw1.WriteLine.
.Write("TEXT") will write the word "TEXT" to the file but it will not write anything else
.WriteLine("TEXT") will write the word "TEXT" followed by the new-line {VbGtLf} character

DigitalDan.co.uk ... Hits = 218