我试图检查文件是否存在,如果是这样,它什么也不做。如果文件不存在则创建文本文件。然后我想写文本到该文件。我在哪里错了这个代码?我只是试图写入多行文本文件,该部分不工作。它正在创建文本文件...只是没有写入它。使用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
什么放在第一位让你觉得有什么不对的代码?你有错误还是意外的行为? – 2013-02-15 21:55:34
是的,“textfile.txt”在目录文件夹中创建,但它不会让我写入文件。我得到一个错误,说mscorlib.dll 中发生类型'System.IO.IOException'的第一次机会异常进程失败:System.Windows.Forms.MouseEventArgs – 2013-02-15 21:57:47
这是否编译?你有一个没有'Catch'或'Finally'的'Try'。 – 2013-02-15 22:06:27