vb.net涵盖所以文件操作函数实例代码汇总
时间: 2023-08-14 20:06:31 浏览: 99
以下是一些常见的文件操作函数实例代码汇总:
1. 创建文件:
```vb
Dim filePath As String = "C:\test.txt"
File.Create(filePath)
```
2. 写入文件:
```vb
Dim filePath As String = "C:\test.txt"
Dim content As String = "Hello, world!"
File.WriteAllText(filePath, content)
```
3. 读取文件:
```vb
Dim filePath As String = "C:\test.txt"
Dim content As String = File.ReadAllText(filePath)
```
4. 复制文件:
```vb
Dim sourcePath As String = "C:\source.txt"
Dim destPath As String = "C:\dest.txt"
File.Copy(sourcePath, destPath)
```
5. 移动文件:
```vb
Dim sourcePath As String = "C:\source.txt"
Dim destPath As String = "C:\dest.txt"
File.Move(sourcePath, destPath)
```
6. 删除文件:
```vb
Dim filePath As String = "C:\test.txt"
File.Delete(filePath)
```
7. 判断文件是否存在:
```vb
Dim filePath As String = "C:\test.txt"
If File.Exists(filePath) Then
' 文件存在
Else
' 文件不存在
End If
```
8. 获取文件信息:
```vb
Dim filePath As String = "C:\test.txt"
Dim fileInfo As FileInfo = New FileInfo(filePath)
Console.WriteLine($"文件名:{fileInfo.Name}")
Console.WriteLine($"文件大小:{fileInfo.Length} 字节")
Console.WriteLine($"创建时间:{fileInfo.CreationTime}")
Console.WriteLine($"修改时间:{fileInfo.LastWriteTime}")
```
以上是一些常用的文件操作函数实例代码,您可以根据您的具体需求进行调整和使用。
阅读全文