Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 删除目录删除字符串中最后一个为空的目录_Vb.net_Visual Studio 2012_Directory.delete - Fatal编程技术网

Vb.net 删除目录删除字符串中最后一个为空的目录

Vb.net 删除目录删除字符串中最后一个为空的目录,vb.net,visual-studio-2012,directory.delete,Vb.net,Visual Studio 2012,Directory.delete,下面的子项搜索某些文件夹并删除任何空文件夹(除非该文件夹称为Bars或Backup) sPath设置为c:\temp\working\,如果为空,则应扫描和删除的文件夹是working 但是,如果子目录中的所有子目录都是空的,并且都已被删除,那么文件夹Working就会被删除,这不是我需要的 有没有关于如何停止此操作的建议,或者如果目录进入该阶段,是否需要重新创建目录(不理想) 谢谢解决此问题的第一种方法是传递起始根目录,如果目录是起始根目录,则不要删除它 Public Sub DeleteEm

下面的子项搜索某些文件夹并删除任何空文件夹(除非该文件夹称为Bars或Backup)

sPath设置为
c:\temp\working\
,如果为空,则应扫描和删除的文件夹是
working

但是,如果子目录中的所有子目录都是空的,并且都已被删除,那么文件夹
Working
就会被删除,这不是我需要的

有没有关于如何停止此操作的建议,或者如果目录进入该阶段,是否需要重新创建目录(不理想)


谢谢

解决此问题的第一种方法是传递起始根目录,如果目录是起始根目录,则不要删除它

Public Sub DeleteEmptyFolders(ByVal sPath As String, ByVal sRoot As String)

    Dim SubDirectories() As String = Directory.GetDirectories(sPath )
    For Each strDirectory As String In SubDirectories
        DeleteEmptyFolders(strDirectory, sRoot)
    Next
    If Not (UCase(Path.GetDirectoryName(sPath)).Contains("BACKUP")) Or (UCase(Path.GetDirectoryName(sPath)).Contains("BARS")) Then
        If Directory.GetFiles(sPath).Length + Directory.GetDirectories(sPath).Length = 0 Then
            if sPath <> sRoot Then
                Directory.Delete(sPath)
                Console.WriteLine("Deleting empty folder: " & sPath)
            End If
        End If
    End If
End Sub

似乎不喜欢第二个变量也是pathMy bad,从不需要的GetDirectories中删除了sRoot
Public Sub DeleteEmptyFolders(ByVal sPath As String, ByVal sRoot As String)

    Dim SubDirectories() As String = Directory.GetDirectories(sPath )
    For Each strDirectory As String In SubDirectories
        DeleteEmptyFolders(strDirectory, sRoot)
    Next
    If Not (UCase(Path.GetDirectoryName(sPath)).Contains("BACKUP")) Or (UCase(Path.GetDirectoryName(sPath)).Contains("BARS")) Then
        If Directory.GetFiles(sPath).Length + Directory.GetDirectories(sPath).Length = 0 Then
            if sPath <> sRoot Then
                Directory.Delete(sPath)
                Console.WriteLine("Deleting empty folder: " & sPath)
            End If
        End If
    End If
End Sub
DeleteEmptyFolders("D:\temp", "D:\temp")