Reputation: 19
Yeah, I've been trying to wrap my head around this but, I want to use a for loop to allow StreamWriter to write one line at a time; this is what i've come up with so far:
Imports System.IO
Imports Microsoft.VisualBasic.FileIO
Module writeOneLineAtATime
Sub Main()
Dim fileWriter As StreamWriter
Dim lineOfCode As String
Dim fileName, filePath As String
'Get that file location, dog.
fileName = "someonewashere.txt"
filePath = "Data\"
Debug.Print("File location: " + filePath + fileName)
fileWriter = My.Computer.FileSystem.OpenTextFileWriter(filePath + fileName, True)
Do
fileWriter.WriteLine("2butt")
fileWriter.Close()
Loop
Console.ReadLine()
End Sub
End Module
Upvotes: 1
Views: 2090
Reputation: 27342
There is an easy way to do this which is as follows:
Dim fullyQualifiedPath = Path.Combine(filePath, fileName)
Using sw As New StreamWriter(fullyQualifiedPath)
sw.WriteLine("Line1")
sw.WriteLine("Line2)
' etc.
End Using
Wrapping the code in a Using
block means that you don't have to Close
or Dispose
of it when you are done as that is all taken care of.
Also using Path.Combine
is safer than concatenating the path and file yourself, but anytime you are concatenating strings you should use &
instead of +
.
Upvotes: 3