vb.net读写文本文档的方法
时间: 2023-09-10 17:15:38 浏览: 64
可以使用以下代码来读取文本文件:
```
Dim filePath As String = "C:\example.txt"
Dim fileContent As String = ""
' 读取文本文件内容
Using reader As New StreamReader(filePath)
fileContent = reader.ReadToEnd()
End Using
' 输出文本文件内容
Console.WriteLine(fileContent)
```
同样,可以使用以下代码来写入文本文件:
```
Dim filePath As String = "C:\example.txt"
Dim fileContent As String = "Hello, world!"
' 写入文本文件内容
Using writer As New StreamWriter(filePath)
writer.Write(fileContent)
End Using
```
需要注意的是,如果文件已经存在,写入操作将会覆盖原有内容。如果需要追加内容,可以使用以下代码:
```
Dim filePath As String = "C:\example.txt"
Dim fileContent As String = "Hello, world!"
' 追加文本文件内容
Using writer As New StreamWriter(filePath, True)
writer.Write(fileContent)
End Using
```
在此代码中,第二个参数 `True` 表示在文件末尾追加内容。