VB.Net-如何更改包含子文件夹的文件夹属性&;文件夹

VB.Net-如何更改包含子文件夹的文件夹属性&;文件夹,vb.net,attributes,subdirectory,Vb.net,Attributes,Subdirectory,如何更改或删除选定文件夹的属性,包括所有子文件夹和文件 我使用了以下代码: System.IO.SetAttribute(FolderBrowserDialog1.SelectedPath,IO.FileAttribute.Hidden) 但它只更改选定的文件夹属性,而不更改子文件夹和文件您可以递归地循环子文件夹。我认为操作系统也会递归地这样做 Private Function getAllFolders(ByVal directory As String) As List(of String

如何更改或删除选定文件夹的属性,包括所有子文件夹和文件

我使用了以下代码:

System.IO.SetAttribute(FolderBrowserDialog1.SelectedPath,IO.FileAttribute.Hidden)

但它只更改选定的文件夹属性,而不更改子文件夹和文件

您可以递归地循环子文件夹。我认为操作系统也会递归地这样做

Private Function getAllFolders(ByVal directory As String) As List(of String)
        'Create object
        Dim fi As New IO.DirectoryInfo(directory)
        'Change main folder attribute
        System.IO.SetAttribute(directory,IO.FileAttribute.Hidden )
        'List to store paths
        Dim Folders As New List(Of String)
        'Loop through subfolders
        For Each subfolder As IO.DirectoryInfo In fi.GetDirectories()
            'Add this folders name

           Folders.Add(subfolder.FullName)

            'Recall function with each subdirectory
            For Each s As String In getAllFolders(subfolder.FullName)
                Folders.Add(s)
                'Change subfolders attribute
                System.IO.SetAttribute(s,IO.FileAttribute.Hidden )
            Next

        Next


        Return Folders

 End Function

您可以递归地循环子文件夹。我认为操作系统也会递归地这样做

Private Function getAllFolders(ByVal directory As String) As List(of String)
        'Create object
        Dim fi As New IO.DirectoryInfo(directory)
        'Change main folder attribute
        System.IO.SetAttribute(directory,IO.FileAttribute.Hidden )
        'List to store paths
        Dim Folders As New List(Of String)
        'Loop through subfolders
        For Each subfolder As IO.DirectoryInfo In fi.GetDirectories()
            'Add this folders name

           Folders.Add(subfolder.FullName)

            'Recall function with each subdirectory
            For Each s As String In getAllFolders(subfolder.FullName)
                Folders.Add(s)
                'Change subfolders attribute
                System.IO.SetAttribute(s,IO.FileAttribute.Hidden )
            Next

        Next


        Return Folders

 End Function

所有子文件夹和文件都可以按如下方式枚举:

If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then

    Dim di = New IO.DirectoryInfo(FolderBrowserDialog1.SelectedPath)
    di.Attributes = di.Attributes Or FileAttributes.Hidden

    For Each i In di.EnumerateFileSystemInfos("*", SearchOption.AllDirectories)
        i.Attributes = i.Attributes Or FileAttributes.Hidden
    Next
End If

另一种方法是:


我希望它比枚举所有文件项以及分别获取和设置每个文件项的属性更快,但此方法的另一个优点是,默认情况下,shell函数不会等待命令完成,并且您的程序可以在不等待的情况下继续

所有子文件夹和文件都可以按如下方式枚举:

If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then

    Dim di = New IO.DirectoryInfo(FolderBrowserDialog1.SelectedPath)
    di.Attributes = di.Attributes Or FileAttributes.Hidden

    For Each i In di.EnumerateFileSystemInfos("*", SearchOption.AllDirectories)
        i.Attributes = i.Attributes Or FileAttributes.Hidden
    Next
End If

另一种方法是:


我希望它比枚举所有文件项以及分别获取和设置每个文件项的属性更快,但此方法的另一个优点是,默认情况下,shell函数不会等待命令完成,并且您的程序可以在不等待的情况下继续

您需要从用户选择的目录中循环所有子目录,一个递归函数。。。现在,您只需立即为所选目录更改它。在设置属性之前,先循环检索每个目录及其文件,然后设置该属性。您需要从用户选择的目录中循环所有子目录,一个递归函数。。。现在,您只需立即为所选目录更改它。在设置属性之前,先循环检索每个目录及其文件,然后设置该属性。