如果文件存在VB.NET,如何删除List的元素

时间:2016-03-19 03:38:28

标签: vb.net

如果文件中存在相同的元素,我如何删除List(Of String)中的元素或项目?我有这个代码,但它不起作用,它不会删除任何东西。

dest有时候就像这个D:\Test

Dim dest As String = Label1.Text

For i = 0 to myfiles.Count - 1
   If File.Exists(Path.Combine(dest, myfiles(i))) Then
       myfiles.Remove(myfiles(i))
   End If
Next

我使用FileSystemWatcherList(Of String)中添加文件。

Sub FileLogs()
    watchfolder = New System.IO.FileSystemWatcher()

    Dim src As String = Label5.Text

    watchfolder.Path = src
    watchfolder.IncludeSubdirectories = True

    watchfolder.NotifyFilter = NotifyFilters.DirectoryName
    watchfolder.NotifyFilter = NotifyFilters.FileName Or IO.NotifyFilters.FileName
    watchfolder.NotifyFilter = NotifyFilters.Size Or IO.NotifyFilters.Size

    AddHandler watchfolder.Changed, AddressOf logattrichanged

    'Start watching for renames of folders and files
    watchfolder.EnableRaisingEvents = True
End Sub

Private Sub logattrichanged(ByVal sender As Object, ByVal e As FileSystemEventArgs)
    Dim lastWriteTime As DateTime = File.GetLastWriteTime(e.FullPath)
    If lastWriteTime <> lastRead Then
            myfiles.Add(e.Name)
        lastRead = lastWriteTime
    End If
End Sub

1 个答案:

答案 0 :(得分:1)

假设gets.chomp.titleize 是有效路径,dest是有效文件名的集合,请按以下步骤操作:

myfiles

现在你可以使用一些LINQ来缩短它,但我会把它作为练习留给你。