如何更改或删除所选文件夹的属性,包括所有子文件夹&文件。
我使用了以下代码:
System.IO.SetAttribute(FolderBrowserDialog1.SelectedPath,IO.FileAttribute.Hidden)
但它只更改选定的文件夹属性而不是子文件夹&文件
答案 0 :(得分: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
答案 1 :(得分: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)
我希望它比枚举所有文件条目以及分别获取和设置每个文件条目的属性更快,但这种方法的另一个优点是默认情况下shell函数不会等待命令完成而你的程序可以继续,没有等待。