Ad space (banner)
๐Ÿ”ทVB.NET Lessons
Lesson 52 / 68

The basics of reading and writing files

This lesson covers reading and writing files in VB.NET, with the aim of saving and loading text from a program.

File.WriteAllText() writes a string to a file and File.ReadAllText() reads one back. Putting Imports System.IO at the top lets you use these under their short names.

The sample code saves the contents of a memo with File.WriteAllText("memo.txt", ...) and reads the same file straight back with File.ReadAllText("memo.txt"). Note the line break inserted with vbLf.

A common early stumble is handling a missing file or missing write permission. Real applications are expected to wrap file operations in Try...Catch.

In professional work, file handling is needed in almost every application — saving configuration, writing to a log, reading CSV data.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run VB.NET directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)