Write File in VB.NET

How to write a file in VB.NET?

Explanation

Write a File in Vb.net 2008.

To write a file, the StreamWriter class and File.CreateText method is used to create a file first. Then the WriteLine method is used to write text.
Example:

Imports System.IO
Module Module1
Sub Main()
Dim w As StreamWriter
Dim r As StreamReader
w = File.CreateText("C:write.txt")
w.WriteLine("Welcome to.Net Programming")
w.WriteLine("This is an example to write to a File")
w.Flush()
w.Close()
r = File.OpenText("c:write.txt")
Console.WriteLine(r.ReadToEnd)
Console.Read()
r.Close()
End Sub
End Module
Result:

Welcome to.Net Programming
This is an example to write to a File

In the above example, the file 'write.txt' is created using the File.CreateText method. The same file is read with the File.OpenText method.

Visual Basic Tutorial


Ask Questions

Ask Question