0% found this document useful (0 votes)
24 views

Reads and Displays Only The First Line: Before Module

These are just the basic codes for File Handling that i know are working. None of the ones Sir Khurram/ Sir Asim provided work in VB.net, for me at least.

Uploaded by

Anonymous nkp2V2
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Reads and Displays Only The First Line: Before Module

These are just the basic codes for File Handling that i know are working. None of the ones Sir Khurram/ Sir Asim provided work in VB.net, for me at least.

Uploaded by

Anonymous nkp2V2
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Before Module:

Imports System
Imports System.IO

Reading the first line from a file named text located on the Desktop [Reads and
displays |on console| only the first line]:
Dim file As StreamReader
Dim text As String
file = New StreamReader("C:\Users\Daud Cheema\Desktop\text.txt")
text = File.ReadLine()
Console.WriteLine(text)
Console.ReadLine()
file.Close()

Reading all the lines from a file named text located on the Desktop [Reads and displays
|on console| all the lines till the text in the file finishes]:
Dim file As StreamReader
Dim text As String
file = New StreamReader("C:\Users\Daud Cheema\Desktop\text.txt")

Do Until file.EndOfStream EndOfStream function detects the end of a file


text = file.ReadLine()
Console.WriteLine(text)
Loop
Console.ReadLine()
file.Close()

Writing to a file named text located on the Desktop [This will remove all text present in the file,
if any and replace it with the one input by the user]:

Dim file As StreamWriter


Dim text As String
file = New StreamWriter("C:\Users\Daud Cheema\Desktop\text.txt") End here if you only
want to create the file

Console.WriteLine("Please input desired text!")


text = Console.ReadLine()
file.WriteLine(text)
Console.ReadLine()
file.Close()
Appending a file named text located on the Desktop [This will add text to the NEXT line in the
file without removing the original text, if present]:

Dim file As StreamWriter


Dim text As String
file = New StreamWriter("C:\Users\Daud Cheema\Desktop\text.txt", True)
Console.WriteLine("Please input desired text!")
text = Console.ReadLine()
file.WriteLine(text)
Console.ReadLine()
file.Close()

You might also like