Writing to a text file Learn programming Visual Basic (VB.net)

Lesson:

File Management


Exercise:

Writing to a text file 11


Objetive:

Create a program to ask the user for several sentences (until they just press Enter) and store them in a text file named "sentences.txt"


Code:

Imports System
Imports System.IO
Namespace FileWrite
    Class Program
        Private Shared Sub Main(ByVal args As String())
            Dim sentence As String = " "
            Dim myfile As StreamWriter
            myfile = File.CreateText("test.txt")

            Do
                Console.Write("Enter a sentence: ")
                sentence = Console.ReadLine()

                If sentence.Length <> 0 Then
                    myfile.WriteLine(sentence)
                End If
            Loop While sentence.Length <> 0

            myfile.Close()
        End Sub
    End Class
End Namespace