You are on page 1of 5

Chapter 5

27/09/2015
Bale, Robe Ethiopia
6/7/2023 EDP CH5 compiled by ydereje2020@gmail.com 1
5.1. How to open a Text File
‒ To open a text file in VB, you can use the OpenText method. This method opens a specified file and returns a TextStream object that can be used to read from, write to, or
append to the file. Here is an example of how to use this method:
‒ 'Dim MyFile As IO.StreamReader
‒ Dim MyFile As Object
‒ Dim FileName As String
‒ FileName = "D:\\test.txt"
‒ MyFile = IO.File.OpenText(FileName)
‒ RichTextBox1.Text = MyFile.ReadToEnd()
‒ This code opens the file “test.txt” located in the C:\ directory.
Alternatively, you can use the ReadAllText method of the My.Computer.FileSystem object to read the contents of a text file into a string. Here is an example of how to use this method:
‒ Dim myOpenFileDialog As New OpenFileDialog()
‒ myOpenFileDialog.CheckFileExists = True
‒ myOpenFileDialog.DefaultExt = "txt"
‒ myOpenFileDialog.InitialDirectory = "C:\"
‒ myOpenFileDialog.Multiselect = False
‒ If myOpenFileDialog.ShowDialog = DialogResult.OK Then
‒ Me.Text = myOpenFileDialog.FileName
‒ Me.RichTextBox1.Text = My.Computer.FileSystem.ReadAllText(myOpenFileDialog.FileName)
‒ End If
This code reads the contents of the file “test.txt” located in the C:\ directory into a string variable named fileContents.

6/7/2023 EDP CH5 compiled by ydereje2020@gmail.com 2


5.2. Read a file line by line in VB .NET
To read a file line by line in VB .NET, you can use the StreamReader class. Here is an example code snippet that
reads a file line by line:
‒ Imports System.IO
‒ Public Class Form1
‒ Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
‒ Using reader As StreamReader = New StreamReader("D:\\test.txt")
‒ Dim line As String
‒ While Not reader.EndOfStream
‒ line = reader.ReadLine()
‒ ' Do something with the line
‒ RichTextBox1.Text = line
‒ End While
‒ End Using
‒ End Sub
‒ End Class
‒ This code opens a file named test.txt located at C:\ and reads it line by line. The StreamReader class is
used to read the file and the While Not reader.EndOfStream loop is used to read each line until the end of
the file is reached. You can replace “D:\\test.txt" with the path of your file.

6/7/2023 EDP CH5 compiled by ydereje2020@gmail.com 3


5.3. Write to a Text File
‒ To write to a text file in VB .NET from a textbox, you can use the
StreamWriter class. Here is an example code snippet that writes to a file:
‒ Using writer As StreamWriter = New
StreamWriter(“C:\\test.txt", True)
‒ 'writer.WriteLine("Hello World!")
‒ writer.WriteLine(RichTextBox1.Text)
‒ End Using
This code opens a file named test.txt located at C:\\ and writes "Hello World!"
to it or writes the contents of RichTextBox1 to it. The StreamWriter class is
used to write to the file and the True parameter is used to append the text to
the end of the file. You can replace "C:\\test.txt" with the path of your file.

6/7/2023 EDP CH5 compiled by ydereje2020@gmail.com 4


5.4. Copy, Move and Delete a File
‒ To copy, move and delete files using VB .NET Windows Forms application, you
can use the File class. Here is an example code snippet that copies, moves and
deletes a file:
' Copy a file
' Copy the file to a new location without overwriting existing file.
My.Computer.FileSystem.CopyFile("D:\test.txt", "D:\testFile.txt")
'move file
My.Computer.FileSystem.MoveFile("D:\\test.txt","D:\myfolder\test.
txt")
' Delete a file
My.Computer.FileSystem.DeleteFile("D:\testfile.txt")

6/7/2023 EDP CH5 compiled by ydereje2020@gmail.com 5

You might also like