【问题标题】:How to double check if folder is empty`?如何仔细检查文件夹是否为空`?
【发布时间】:2020-05-28 20:49:09
【问题描述】:

我找不到我的问题的解决方案。我的代码正在删除空文件夹,并且通常工作正常,但有一个例外。它遍历每条路径一次,但是如果有一个文件夹(A),其中只有一个空文件夹(B),那么它只会删除文件夹(B),因为文件夹(A)当时不是空的.我怎样才能让它知道一旦文件夹(B)被删除,文件夹(A)将是空的?

我认为DeleteEmptyFolder(folder.FullName) 会解决问题,但它不起作用,因为它不会重复它已经经历过的相同路径。删除directory.GetDirectories.Count = 0 也不起作用,因为它会删除任何其中没有文件的文件夹(即使有另一个文件夹中有文件)

Private Sub DeleteEmptyFolder(ByVal sDirectoryPath As String)

            If IO.Directory.Exists(sDirectoryPath) Then
                Dim directory As New IO.DirectoryInfo(sDirectoryPath)
                If directory.GetDirectories.Count = 0 AndAlso directory.GetFiles.Count = 0 Then
                    directory.Delete(True)
                    Return
                End If
                For Each folder As IO.DirectoryInfo In directory.GetDirectories()
                    DeleteEmptyFolder(folder.FullName)
                Next
           End If
End Sub

我对 VB.Net 还很陌生,如果这是我看不到的明显答案,请原谅。

【问题讨论】:

  • 让我更了解你的最终目标。您想将文件夹路径传递给您的函数,它会搜索所有文件夹和子文件夹并删除空文件夹,最后如果主文件夹不包含任何内容,也会自行删除。我说的对吗?
  • 它基本上应该删除所有空文件夹,包括在此过程中变空的文件夹

标签: vb.net


【解决方案1】:

以下是代码的外观:

Private Sub DeleteEmptyFolder(folderPath As String)
    If Directory.Exists(folderPath) Then
        For Each subFolderPath In Directory.EnumerateDirectories(folderPath)
            DeleteEmptyFolder(subFolderPath)
        Next

        If Directory.EnumerateFiles(folderPath).Any() OrElse
           Directory.EnumerateDirectories(folderPath).Any() Then
            Return
        End If

        Directory.Delete(folderPath)
    End If
End Sub

如果您不需要除路径以外的文件和文件夹的其他信息,那么使用DirectoryInfo 毫无意义。您应该使用EnumerateFilesEnumerateDirectories 而不是GetFilesGetDirectories,除非您特别需要预先获取一组条目。在这种情况下,你绝对不会。假设您有一个包含 1000 个文件的文件夹。这个:

directory.GetFiles.Count = 0

将首先创建一个包含所有 1000 个文件的元素的数组,然后检查其中的元素数。另一方面,这是:

Directory.EnumerateFiles(folderPath).Any()

遇到第一个文件时会立即返回True,忽略其他999。您只关心文件夹中是否有文件,而不关心有多少。

【讨论】:

  • 我也想到了Any,列出答案。 ?
【解决方案2】:

请试试这个:

    Private Sub deleteEmptyFolders(ByRef folder As String)

        'Does exist such a path?
        If IO.Directory.Exists(folder) Then 'yes

            'Loop over all directories
            For Each subFolder In IO.Directory.GetDirectories(folder)

                'Delete all empty folders
                deleteEmptyFolders(subFolder)
            Next

            'Delete folder if nothing remained in it
            Try
                My.Computer.FileSystem.DeleteDirectory(folder, FileIO.DeleteDirectoryOption.ThrowIfDirectoryNonEmpty)
            Catch ex As Exception
            End Try
        End If
    End Sub

我认为这以一种简单的方式满足您的需求。

【讨论】:

  • 没有类型的方法参数是错误代码。您应该始终拥有Option Strict On。此外,没有理由将该参数声明为 ByRef
  • 你是对的类型。我错过了。但是关于ByRef,一般来说,当你不改变输入的时候,就没有必要复制那个了。因此,最好通过引用来传递它,而不是创建输入的副本。
  • “当你不改变输入” ByRef,除非我需要更改返回的值。这是为了防止当代码很深时出现问题。
  • 你对ByRef 的看法是完全错误的。这不是 VB6。您需要了解值类型和引用类型之间的区别。当您按值传递时,会创建变量内容的副本。对于引用类型,变量内容是对对象的引用,而不是对象本身,因此不会对该对象进行复制。这就是引用类型的全部意义所在。 ByVal 是默认值是有原因的。对于引用类型(类和委托),您通过引用传递的唯一时间是如果您想在方法内部设置参数本身并将其反映在方法外部。
  • 请注意,尽管字符串的行为更像是值而不是引用(与“普通”引用类型不同,在例程中更改 folder 不会在例程之外看到),它们仍然 是引用并且在传递给ByVal参数时不会复制。
【解决方案3】:

我通过移动一些代码解决了这个问题。

        Private Sub DeleteEmptyFolder(ByVal sDirectoryPath As String)

                If IO.Directory.Exists(sDirectoryPath) Then
                    Dim directory As New IO.DirectoryInfo(sDirectoryPath)
                    For Each folder As IO.DirectoryInfo In directory.GetDirectories()
                        DeleteEmptyFolder(folder.FullName)
                    Next
                    If directory.GetDirectories.Count = 0 AndAlso directory.GetFiles.Count = 0 Then
                        directory.Delete(True)
                        Return
                    End If
                End If
            End If

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-19
    • 1970-01-01
    • 2017-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多