C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Step 1: We use the StreamReader. It is best to use the Using statement, which provides for system-level cleanup of resources.
Step 2: Here we begin using the StreamReader inside a Do While (True) loop construct.
Step 3: We read in a line with ReadLine() and test to see if the string is Nothing. We exit if it is Nothing.
VB.NET program that uses StreamReader type
Imports System.IO
Module Module1
Sub Main()
' Step 1: create StreamReader for the file.
Using reader As StreamReader = New StreamReader("C:\programs\file.txt")
' Step 2: begin do-loop.
Do
' Step 3: call ReadLine.
Dim line As String = reader.ReadLine
' ... Exit the loop if Nothing.
If line Is Nothing Then
Exit Do
End If
Console.WriteLine(line)
Loop
End Using
End Sub
End Module
Output
Thank you
Friend
Thus: You could implement a function equivalent to ReadAllText with the StreamReader type.
VB.NET program that uses ReadAllText
Imports System.IO
Module Module1
Sub Main()
Dim value As String = File.ReadAllText("C:\file.txt")
Console.WriteLine(value.Length)
End Sub
End Module
Output
40
Step 1: We call ReadAllLines with an argument equal to the file name. It places the lines in a String array.
Step 2: A For-Each loop is used on that String, providing each line in the file for usage elsewhere in code.
Info: File.ReadLines is different from ReadAllLines. It reads one line in at a time—this uses less memory.
File.ReadLinesVB.NET program that uses ReadAllLines
Imports System.IO
Module Module1
Sub Main()
' Step 1: read the file into an array.
' ... Create the required file if needed.
Dim array As String() = File.ReadAllLines("file.txt")
' Step 2: use For-Each loop.
For Each line As String In array
' Use the line.
Dim length As Integer = line.Length
Next
End Sub
End Module
Then: You can use each line of the file in a List collection. You can apply all List functions.
ListVB.NET program that uses ToList method on file array
Imports System.IO
Module Module1
Sub Main()
' Create a list reference variable.
' ... Then read all lines in the file.
' ... Convert the array to a List.
Dim list As List(Of String) =
File.ReadAllLines("file.txt").ToList
Console.WriteLine(list.Count)
End Sub
End Module
Output
5
Tip: You could instead use StreamReader code above, and then increment an integer on each successful call to the ReadLine method.
VB.NET program that gets line count
Imports System.IO
Module Module1
Sub Main()
' Get the length of the file.
' ... Not the fastest way to get the line count.
Dim length As Integer = File.ReadAllLines("file.txt").Length
Console.WriteLine(length)
End Sub
End Module
Output
5
VB.NET program that uses WriteAllLines
Imports System.IO
Module Module1
Sub Main()
' Create an array of three elements.
Dim array As String() = New String() {"cat", "dog", "arrow"}
' Write the array to a file.
File.WriteAllLines("file.txt", array)
End Sub
End Module
File contents: file.txt
cat
dog
arrow
File.ReadAllBytes: Useful for files not stored as plain text. You can open images or movies with this method.
File.ReadAllLines: Microsoft: "Opens a file, reads all lines of the file with the specified encoding, and closes the file."
File.ReadAllText: Returns the contents of the text file at the specified path as a string. Useful for plain text or settings files.
File.WriteAllBytes: Useful for files such as images that were created or mutated in memory.
File.WriteAllLines: Stores a string array in the specified file, overwriting the contents. Shown in an example.
File.WriteAllText: Writes the contents of a string to a text file. One of the simplest ways to persist text data.
File.AppendAllText: Use to append the contents string to the file at the path. It creates a new file if needed.
Recursive: Newer versions of the .NET Framework include a method that handles recursive directory file lists. We show an older solution.
Recursive FilesFileInfo: It is often necessary to get the size of a file in bytes. The FileInfo type is often needed for this purpose.
File Size