2016-11-20 186 views
1

如何更改或刪除包括所有子文件夾&文件的選定文件夾的屬性。VB.Net - 如何更改文件夾包括子文件夾和文件的屬性

我用下面的代碼:

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

但它僅更改所選文件夾的屬性沒有子文件夾&文件

+0

您需要將所有子目錄從用戶選擇的目錄中循環,遞歸函數......現在,您只需將其更改爲所選目錄即可。在設置屬性之前,循環檢索每個目錄及其文件,然後設置該屬性。 – Codexer

回答

0

你也可以遍歷子文件夾遞歸。我認爲這個操作系統也是遞歸的!

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 
0

所有子文件夾和文件可以列舉如下:

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 

另一種方式可以與attrib.exe

Dim cmd = "attrib +H """ & FolderBrowserDialog1.SelectedPath.TrimEnd("\"c) 

Shell("cmd /c " & cmd & """ & " & cmd & "\*"" /S /D", AppWinStyle.Hide) 

我希望它是比枚舉所有快文件條目,並分別獲取和設置每個屬性,但這種方法的另一個優點d是默認情況下,shell函數不會等待命令完成,並且程序可以繼續而不用等待。

相關問題