Запись нескольких строк в текстовый файл с помощью Visual Basic
Я пытаюсь проверить, существует ли файл, если это так, он ничего не делает. Если файл не существует, создает текстовый файл. Затем я хочу написать текст в этот файл. Куда я не так с этим кодом? Я просто пытаюсь записать несколько строк в текстовый файл, и эта часть не работает. Он создает текстовый файл ... просто не пишет в него.
Dim file As System.IO.FileStream
Try
' Indicate whether the text file exists
If My.Computer.FileSystem.FileExists("c:\directory\textfile.txt") Then
Return
End If
' Try to create the text file with all the info in it
file = System.IO.File.Create("c:\directory\textfile.txt")
Dim addInfo As New System.IO.StreamWriter("c:\directory\textfile.txt")
addInfo.WriteLine("first line of text")
addInfo.WriteLine("") ' blank line of text
addInfo.WriteLine("3rd line of some text")
addInfo.WriteLine("4th line of some text")
addInfo.WriteLine("5th line of some text")
addInfo.close()
End Try