VB.Net检查文件是否存在及删除文件

Dim FileExists As Boolean

Dim filePath = "D:\Book1.xls"

fileExists = My.Computer.FileSystem.FileExists(filePath)

If fileExists = False Then

    MsgBox("文件不存在")

Else

    '文件存在则删除文件

    My.Computer.FileSystem.DeleteFile(filePath, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.DeletePermanently)

End If

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是 VB.NET 中的文件操作函数的完整列表及其示例代码: 1. `File.Exists` - 检查文件是否存在 ``` Dim filePath As String = "C:\example.txt" If File.Exists(filePath) Then Console.WriteLine("File exists") Else Console.WriteLine("File does not exist") End If ``` 2. `File.ReadAllText` - 读取文本文件的内容 ``` Dim filePath As String = "C:\example.txt" Dim fileContents As String = File.ReadAllText(filePath) Console.WriteLine(fileContents) ``` 3. `File.ReadAllLines` - 读取文本文件的所有行 ``` Dim filePath As String = "C:\example.txt" Dim fileLines As String() = File.ReadAllLines(filePath) For Each line As String In fileLines Console.WriteLine(line) Next ``` 4. `File.ReadAllText` - 读取二进制文件的内容 ``` Dim filePath As String = "C:\example.bin" Dim fileBytes As Byte() = File.ReadAllBytes(filePath) For Each b As Byte In fileBytes Console.Write(b & " ") Next ``` 5. `File.WriteAllText` - 创建一个新文件并向其中写入文本 ``` Dim filePath As String = "C:\example.txt" Dim fileContents As String = "Hello, world!" File.WriteAllText(filePath, fileContents) ``` 6. `File.WriteAllLines` - 创建一个新文件并向其中写入多行文本 ``` Dim filePath As String = "C:\example.txt" Dim fileLines As String() = {"Line 1", "Line 2", "Line 3"} File.WriteAllLines(filePath, fileLines) ``` 7. `File.WriteAllBytes` - 创建一个新文件并向其中写入二进制数据 ``` Dim filePath As String = "C:\example.bin" Dim fileBytes As Byte() = {1, 2, 3, 4, 5} File.WriteAllBytes(filePath, fileBytes) ``` 8. `File.AppendAllText` - 向现有文件中追加文本 ``` Dim filePath As String = "C:\example.txt" Dim fileContents As String = "New line of text" File.AppendAllText(filePath, fileContents) ``` 9. `File.AppendAllLines` - 向现有文件中追加多行文本 ``` Dim filePath As String = "C:\example.txt" Dim fileLines As String() = {"Line 4", "Line 5", "Line 6"} File.AppendAllLines(filePath, fileLines) ``` 10. `File.Copy` - 复制文件 ``` Dim sourcePath As String = "C:\example.txt" Dim destinationPath As String = "C:\example_copy.txt" File.Copy(sourcePath, destinationPath) ``` 11. `File.Move` - 移动文件 ``` Dim sourcePath As String = "C:\example.txt" Dim destinationPath As String = "C:\ExampleFolder\example.txt" File.Move(sourcePath, destinationPath) ``` 12. `File.Delete` - 删除文件 ``` Dim filePath As String = "C:\example.txt" File.Delete(filePath) ``` 这些是 VB.NET 中可用的文件操作函数及其示例代码。请注意,这些函数不是全部的文件操作函数,但是它们涵盖了大部分常见的文件操作需求。如果您需要执行更高级的文件操作,请查看 .NET Framework 中其他的文件操作类和函数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值