vb.net涵盖所有全部文件操作函数实例代码汇总
时间: 2023-09-19 20:05:51 浏览: 149
以下是一些常用的 VB.NET 文件操作函数实例代码汇总:
1. 创建新文件:
```
File.Create("C:\test.txt")
```
2. 写入文件:
```
Dim sw As StreamWriter = File.CreateText("C:\test.txt")
sw.WriteLine("Hello World")
sw.Close()
```
3. 读取文件:
```
Dim sr As StreamReader = File.OpenText("C:\test.txt")
Dim line As String = sr.ReadLine()
While Not line Is Nothing
Console.WriteLine(line)
line = sr.ReadLine()
End While
sr.Close()
```
4. 复制文件:
```
File.Copy("C:\source.txt", "C:\destination.txt")
```
5. 移动文件:
```
File.Move("C:\source.txt", "C:\destination.txt")
```
6. 删除文件:
```
File.Delete("C:\test.txt")
```
7. 检查文件是否存在:
```
If File.Exists("C:\test.txt") Then
Console.WriteLine("File exists.")
End If
```
8. 获取文件信息:
```
Dim fileInfo As New FileInfo("C:\test.txt")
Console.WriteLine("File name: " & fileInfo.Name)
Console.WriteLine("File size: " & fileInfo.Length)
Console.WriteLine("Last modified: " & fileInfo.LastWriteTime)
```
这些函数可以帮助您完成大多数文件操作任务。请注意,这些代码仅供参考,您可能需要根据自己的需求进行修改。
阅读全文