Display file contents Learn programming Visual Basic (VB.net)

Lesson:

File Management


Exercise:

Display file contents


Objetive:

Create a program to display all the contents of a text file on screen (note: you must use a StreamReader). The name of the file will be entered in the command line or (if there is no command line present) asked to the user by the program.


Code:

Imports System
Imports System.IO
Namespace Reader
    Class Program
        Private Shared Sub Main()
            Console.Write("Enter name of file: ")
            Dim nameFile As String = Console.ReadLine()
            Dim myfile As StreamReader

            Try
                myfile = File.OpenText(nameFile)
                Dim line As String = " "

                Do
                    line = myfile.ReadLine()

                    If line IsNot Nothing Then
                        Console.WriteLine(line)
                    End If
                Loop While line IsNot Nothing

            Catch e As Exception
                Console.WriteLine("Error al intentar abir el fichero.")
            End Try

            Console.ReadLine()
        End Sub
    End Class
End Namespace

Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.