vb.net文件操作函数实例代码汇总
时间: 2023-08-11 18:06:51 浏览: 123
以下是一些 VB.NET 文件操作函数的示例代码:
1. 创建一个新文件并向其中写入文本:
```
Dim filePath As String = "C:\example.txt"
Dim fileContents As String = "Hello, world!"
File.WriteAllText(filePath, fileContents)
```
2. 读取文本文件的内容:
```
Dim filePath As String = "C:\example.txt"
Dim fileContents As String = File.ReadAllText(filePath)
Console.WriteLine(fileContents)
```
3. 向现有文件中追加文本:
```
Dim filePath As String = "C:\example.txt"
Dim fileContents As String = "New line of text"
File.AppendAllText(filePath, fileContents)
```
4. 复制文件:
```
Dim sourcePath As String = "C:\example.txt"
Dim destinationPath As String = "C:\example_copy.txt"
File.Copy(sourcePath, destinationPath)
```
5. 移动文件:
```
Dim sourcePath As String = "C:\example.txt"
Dim destinationPath As String = "C:\ExampleFolder\example.txt"
File.Move(sourcePath, destinationPath)
```
6. 删除文件:
```
Dim filePath As String = "C:\example.txt"
File.Delete(filePath)
```
这些示例代码可以帮助您开始使用 VB.NET 进行文件操作。请注意,这些代码仅适用于简单的文件操作,而更复杂的操作可能需要使用其他函数或类。
阅读全文