logo  VB.Net - Text Files
This Chapter
File Attributes
Binary File
Column Header
Copy Delete ...
CSV File
File Path
Text File
Chapters
Home Page
Colours, RGB
Computer Specifications
Dates&Times
Disk Drives
Files
Folders
GPS and OS Ref
VB.Net Forms
Image Files
If & Select
List/Array
Mathematics
NuGet
Sound
String Functions
Sun and Moon
User Controls
Validation
DigitalDan Sites
My Other Sites
Contact Site

Note
Some pages
may contain
inaccuracies
Hits=5
Reading Text Files
Read Text File - One Line at a Time
Whenever you do anything with files, you need to watch our for exceptions (errors.) This can occur unexpectedly even in "fully tested" programs e.g. Sytem locks a file, disk is full etc. The "Try ... Catch ... End Try" block detects errors before they crash the program. The problem with Try ... Catch, is that the programmer will need to resolve the problem, (e.g. stop accessing file and report the issue.)

Public Function ReadTextFile_LineByLine(filename As String) As String
 Dim LineOfText As String
 Dim errorMessage As String = String.Empty
 Try
  Using sr As New IO.StreamReader(filename)
   While Not sr.EndOfStream
    LineOfText = sr.ReadLine
    '
    ' Processing for each text line goes here
    '
   End While
  End Using
 Catch ex As Exception
  errorMessage = ex.Message
 End Try
 Return errorMessage
End Function
  
Read Text File - Everything at once
When you know that your program will have to read every line of text, it is usually much faster to read all lines on one go instead of trying to read each line separately. Take care when using this approach, because the entire file will be loaded into computer memory and users have be known to load multi-gigabyte files into legacy computers with very limited RAM. Dont forget to enclose to catch any unexpected errors by placing yor function in a Try Catch block.
There are two approaches to loading entire text files - drop the entire contents into a very long string variable, or load every line into sepatate elements of an array or list(of string).
 
All text into one long string
Best suited to short files and files which do not naturaly separate into multiple lines or paragraphs.

Public Function ReadTextFile_EntireFileToArray(filename As String) As String()
 Return IO.File.ReadAllText(filename)
End Function
'
Public Function ReadTextFile_EntireFileToList(filename As String) As List(Of String)
 Return IO.File.ReadAllText(filename).ToList
End Function
  

All Text into Array or List(Of String)
Best suited to files that normally contain line-feeds (enter/return characters)

Public Function ReadTextFile_EntireFilentoString(filename As String) As String
 Return IO.File.ReadAllLines(filename)
End Function
  

Writing Text Files
Whenever we do anything with files, it is important to handle unexpected errors using a Try..,Catch block.
 
Write Text File - Line By Line

Public Sub WriteTextFile_LineByLine(filename As String, LinesInFile As List(Of String))
 Using sw As New IO.StreamWriter(filename)
  ' IO.StreamWriter(filename) will throw exception if file already exists
  ' IO.StreamWriter(filename, false) will replace file if it already exists
  ' IO.StreamWriter(filename, true) will add lines to file if it already exists
  For i As Integer = 0 To LinesInFile.Count - 1
   sw.WriteLine(LinesInFile.Item(i))
  Next
 End Using
End Sub
  

Write Text File - Contents of a single string

Public Sub WriteTextFile_EntireFileInString(filename As String, fileContents As String)
 IO.File.WriteAllText(filename, fileContents)
End Sub
  

Write Text File - Contents of Array or List(Of String)

Public Sub WriteTextFile_EveryLineAtOnce(filename As String, LinesInFile() As String)
 IO.File.WriteAllLines(filename, LinesInFile)
End Sub
'
Public Sub WriteTextFile_EveryLineAtOnce(filename As String, LinesInFile As List(Of String))
 IO.File.WriteAllLines(filename, LinesInFile)
End Sub
  

DigitalDan.co.uk