Programming Fundamentals/Files/VB.NET
files.vb
edit' This program creates a file, adds data to the file, displays the file,
' appends more data to the file, displays the file, and then deletes the file.
' It will not run if the file already exists.
'
' References:
' https://www.mathsisfun.com/temperature-conversion.html
' https://en.wikibooks.org/wiki/Visual_Basic_.NET
Public Module Files
Sub Main()
Const FILENAME As String = "~file.txt"
If(System.IO.File.Exists(FILENAME)) Then
System.Console.WriteLine("File already exists.\n")
Else
CreateFile(FILENAME)
ReadFile(FILENAME)
AppendFile(FILENAME)
ReadFile(FILENAME)
DeleteFile(FILENAME)
End If
End Sub
Sub CreateFile(Filename As String)
Dim File As System.IO.StreamWriter
Dim C As Single
Dim F As Single
File = System.IO.File.CreateText(Filename)
File.WriteLine("C F")
For C = 0 To 50
F = C * 9 / 5 + 32
File.WriteLine(C.ToString() + " " + F.ToString())
Next
File.Close()
End Sub
Sub ReadFile(Filename As String)
Dim File As System.IO.StreamReader
Dim Line As String
File = System.IO.File.OpenText(Filename)
Do While True
Line = File.ReadLine()
If Line = Nothing Then
Exit Do
End If
Console.WriteLine(Line)
Loop
File.Close()
Console.WriteLine("")
End Sub
Sub AppendFile(Filename As String)
Dim File As System.IO.StreamWriter
Dim C As Single
Dim F As Single
File = System.IO.File.AppendText(Filename)
For C = 51 To 100
F = C * 9 / 5 + 32
File.WriteLine(C.ToString() + " " + F.ToString())
Next
File.Close()
End Sub
Sub DeleteFile(Filename As String)
System.IO.File.Delete(Filename)
End Sub
End Module
Try It
editCopy and paste the code above into one of the following free online development environments or use your own VB.NET compiler / interpreter / IDE.