gpt4 book ai didi

vb.net - 检查文件夹中是否有新文件

转载 作者:行者123 更新时间:2023-12-02 04:27:18 27 4
gpt4 key购买 nike

我需要监视文件夹以查看何时创建新文件,然后处理文件并存档。

它是我正在努力解决的新文件的实际检测...我知道我需要查看 FileSystemWatcher 的东西,但想知道是否有人知道它以这种方式使用的任何示例来帮助我开始?

假设我的文件夹是“C:\Temp\”,我需要在任何带有“.dat”扩展名的文件出现时立即知道。

抱歉,这个含糊的问题,我只是无法通过各种谷歌搜索找到我要找的东西。

提前致谢

最佳答案

您可以使用FileSystemWatcher Class为此:它监听文件系统更改通知,并在目录或目录中的文件发生更改时引发事件。

Imports System
Imports System.IO
Imports System.Diagnostics

Public watchfolder As FileSystemWatcher

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
watchfolder = New System.IO.FileSystemWatcher()
watchfolder.Path = "d:\pdf_record\"
watchfolder.NotifyFilter = IO.NotifyFilters.DirectoryName
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or IO.NotifyFilters.FileName
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or IO.NotifyFilters.Attributes
AddHandler watchfolder.Changed, AddressOf logchange
AddHandler watchfolder.Created, AddressOf logchange
AddHandler watchfolder.Deleted, AddressOf logchange
AddHandler watchfolder.Renamed, AddressOf logrename
watchfolder.EnableRaisingEvents = True
End Sub


Private Sub logchange(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs)
If e.ChangeType = IO.WatcherChangeTypes.Changed Then
MsgBox("File " & e.FullPath & " has been modified" & vbCrLf)
End If
If e.ChangeType = IO.WatcherChangeTypes.Created Then
MsgBox("File " & e.FullPath & " has been created" & vbCrLf)
End If
If e.ChangeType = IO.WatcherChangeTypes.Deleted Then
MsgBox("File " & e.FullPath & " has been deleted" & vbCrLf)
End If
End Sub

Public Sub logrename(ByVal source As Object, ByVal e As System.IO.RenamedEventArgs)
MsgBox("File" & e.OldName & " has been renamed to " & e.Name & vbCrLf)
End Sub

关于vb.net - 检查文件夹中是否有新文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26015325/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com