Applications often need to store information permanently, such as user details, reports, logs, or configuration data. VB.NET File Handling allows a program to create, read, write, and manage files on a computer. In this VB.NET File Handling Tutorial, you'll learn how to use System.IO, FileStream, StreamReader, and StreamWriter for common file operations.
What Is File Handling in VB.NET?
File handling allows a VB.NET application to interact with files stored on a system. The VB.NET System.IO namespace provides classes and methods for performing common VB.NET File Operations, including creating, reading, writing, and managing files.
VB.NET FileStream
VB.NET FileStream provides a way to read and write data as a stream of bytes. It is useful when you need lower-level control over file operations.
Imports System.IO
Using fs As New FileStream("data.txt", FileMode.Create)
Dim data() As Byte = System.Text.Encoding.UTF8.GetBytes("Hello VB.NET")
fs.Write(data, 0, data.Length)
End Using
The Using statement ensures that the file stream is properly released after use.
VB.NET StreamWriter
VB.NET StreamWriter makes writing text files easier.
Imports System.IO
Using writer As New StreamWriter("data.txt")
writer.WriteLine("Welcome to VB.NET")
writer.WriteLine("Learning File Handling")
End Using
This is a simple way to write to a file in VB.NET.
VB.NET StreamReader
VB.NET StreamReader is commonly used to read text from a file.
Imports System.IO
Using reader As New StreamReader("data.txt")
Dim content As String = reader.ReadToEnd()
Console.WriteLine(content)
End Using
This demonstrates how to read a file in VB.NET and display its contents.
VB.NET Create, Read, and Write Files
For everyday text-file operations, the File class provides simple methods such as:
File.Create() – Creates a file.
File.ReadAllText() – Reads all text from a file.
File.WriteAllText() – Creates or overwrites a text file.
File.AppendAllText() – Adds text to an existing file.
These methods are often simpler than using FileStream directly.
Best Practices
Use StreamReader and StreamWriter for straightforward text-file operations.
Use FileStream when you need more control over how data is read or written.
Use Using to ensure streams and other resources are properly disposed.
Check whether a file exists before attempting to read it.
Handle file-related errors using exception handling.
Learning Visual Basic .NET File Handling helps you build applications that can store and retrieve information beyond the lifetime of a program. Continue learning VB.NET File I/O to understand more advanced ways of working with files and streams.
Frequently Asked Questions (FAQs)
Q1: What is the output of writing bytes using FileStream and reading them back with ReadAllText?
If you encode the string "Hello VB.NET" into UTF-8 bytes and write it using FileStream, calling File.ReadAllText("data.txt") will output: Hello VB.NET.
Q2: What happens to existing content when using StreamWriter without appending?
By default, initializing New StreamWriter("data.txt") overwrites the existing file. The output of reading the file afterwards will only contain the newly written lines, as previous content is deleted.
Q3: How do I search for a specific string inside a text file in VB.NET?
You can read all lines using File.ReadAllLines() and search line-by-line using string methods such as Contains():
Dim lines() As String = File.ReadAllLines("data.txt")
For Each line As String In lines
If line.Contains("VB.NET") Then
Console.WriteLine("Found match: " & line)
End If
Next
Q4: How can I search for and list all files in a directory using VB.NET?
You can use the Directory.GetFiles() method with search patterns to find files matching specific criteria:
Dim txtFiles() As String = Directory.GetFiles("C:\MyDir", "*.txt", SearchOption.AllDirectories)
For Each filePath As String In txtFiles
Console.WriteLine(filePath)
Next