VB 定时删除文件夹下所有 txt 文件
以下是一个示例的 VB 代码,可以实现每隔一小时删除指定文件夹下的 txt 文件:
Imports System.IO
Imports System.Threading
Module Module1
Sub Main()
' 设置文件夹路径
Dim folderPath As String = 'C:\YourFolderPath'
' 创建一个定时器,每隔一小时执行一次删除操作
Dim timer As New Timer(AddressOf DeleteTxtFiles, folderPath, 0, 3600000)
' 保持程序运行
Console.ReadLine()
End Sub
Sub DeleteTxtFiles(ByVal folderPath As Object)
Try
' 获取文件夹路径
Dim path As String = DirectCast(folderPath, String)
' 获取文件夹下的所有 txt 文件
Dim txtFiles As String() = Directory.GetFiles(path, "*.txt")
' 删除所有 txt 文件
For Each file As String In txtFiles
File.Delete(file)
Next
' 输出删除结果
Console.WriteLine("Deleted " & txtFiles.Length & " txt files.")
Catch ex As Exception
Console.WriteLine("Error: " & ex.Message)
End Try
End Sub
End Module
请将代码中的 'C:\YourFolderPath' 替换为您希望删除 txt 文件的文件夹路径。代码中的定时器每隔一小时执行一次 DeleteTxtFiles 函数,该函数会删除指定文件夹下的所有 txt 文件。
原文地址: https://www.cveoy.top/t/topic/bER1 著作权归作者所有。请勿转载和采集!