我想不出任何解决方案能像你想的那样简洁,但是如果你担心IO性能,而不是两次查找每个现有的文件,只需读取所有的文件位置并在内存中全部执行即可。
Dim dbFiles As List(Of String)
Dim files As New List(Of String)
files.AddRange(IO.Directory.GetFiles("path", "pattern", IO.SearchOption.AllDirectories))
Dim deletedFiles As New List(Of String)
For Each dbfile As String In dbFiles
If files.Contains(dbfile) Then
files.Remove(dbfile)
Else
deletedFiles.Add(dbfile)
End If
Next
' now deleteFiles should have all files that have been deleted from the file
' system and files should have all new files in the file system
如果存储在数据库中的路径名的大小写可能与文件系统中的不同,那么您可能需要对此进行调整。