使用Visual Basic取消隐藏除访问被拒绝路径之外的所有隐藏文件,文件夹,子文件夹和子文件

时间:2014-01-26 01:17:42

标签: vb.net file-io

我的代码如下所示,取消隐藏所有隐藏文件,文件夹,子文件夹和子文件。问题是,如果任何访问被拒绝路径,它将停止。如何使代码跳过访问被拒绝路径并继续其他路径。

Dim MyDrive As String = "D:\"
Dim FileCounter As Integer = 0
Dim FolderCounter As Integer = 0
Dim DriveObj As New IO.DirectoryInfo(MyDrive)

Dim Files As IO.FileInfo() = DriveObj.GetFiles("*.*", IO.SearchOption.AllDirectories)
Dim Directories As IO.DirectoryInfo() = DriveObj.GetDirectories("*.*", IO.SearchOption.AllDirectories)

    Dim Filename As IO.FileSystemInfo
        For Each Filename In Files
            On Error Resume Next
            If (IO.File.GetAttributes(Filename.FullName) And IO.FileAttributes.Hidden) = IO.FileAttributes.Hidden Then
                ' Show the file.
                IO.File.SetAttributes(Filename.FullName, IO.FileAttributes.Normal)
                FileCounter = FileCounter + 1
            End If
        Next

        Dim DirectoryName As IO.DirectoryInfo
        For Each DirectoryName In Directories
            On Error Resume Next
            If (IO.File.GetAttributes(DirectoryName.FullName) And IO.FileAttributes.Hidden) = IO.FileAttributes.Hidden Then
                ' Show the folder.
                IO.File.SetAttributes(DirectoryName.FullName, IO.FileAttributes.Normal)
                FolderCounter = FolderCounter + 1
            End If
        Next

1 个答案:

答案 0 :(得分:1)

当我测试代码时,我意识到访问被拒绝时DriveObj.GetFiles(...)DriveObj.GetDirectories(...)正在停止,而不是For循环。要解决这个问题,你必须使用递归文件和目录列表。

Private Function GetFilesRecursive(ByVal initial As String) As List(Of String)
    ' This list stores the results.
    Dim result As New List(Of String)

    ' This stack stores the directories to process.
    Dim stack As New Stack(Of String)

    ' Add the initial directory
    stack.Push(initial)

    ' Continue processing for each stacked directory
    Do While (stack.Count > 0)
        ' Get top directory string
        Dim dir As String = stack.Pop
        Try
            ' Add all immediate file paths
            result.AddRange(Directory.GetFiles(dir, "*.*"))

            ' Loop through all subdirectories and add them to the stack.
            Dim directoryName As String
            For Each directoryName In Directory.GetDirectories(dir)
                stack.Push(directoryName)
            Next

        Catch ex As Exception
        End Try
    Loop

    ' Return the list
    Return result
End Function

Private Function GetDirectoriesRecursive(ByVal initial As String) As List(Of String)
    ' This list stores the results.
    Dim result As New List(Of String)

    ' This stack stores the directories to process.
    Dim stack As New Stack(Of String)

    ' Add the initial directory
    stack.Push(initial)

    ' Continue processing for each stacked directory
    Do While (stack.Count > 0)
        ' Get top directory string
        Dim dir As String = stack.Pop
        Try
            ' Add all immediate file paths
            result.AddRange(Directory.GetDirectories(dir, "*.*"))

            ' Loop through all subdirectories and add them to the stack.
            Dim directoryName As String
            For Each directoryName In Directory.GetDirectories(dir)
                stack.Push(directoryName)
            Next

        Catch ex As Exception
        End Try
    Loop

    ' Return the list
    Return result
End Function

Private Sub Unhide()
    Dim MyDrive As String = "D:\"
    Dim FileCounter As Integer = 0
    Dim FolderCounter As Integer = 0

    Dim Files As List(Of String) = GetFilesRecursive(MyDrive)
    Dim Directories As List(Of String) = GetDirectoriesRecursive(MyDrive)

    For Each Filename In Files
        If (IO.File.GetAttributes(Filename) And IO.FileAttributes.Hidden) = IO.FileAttributes.Hidden Then
            'Show the file.
            IO.File.SetAttributes(Filename, IO.FileAttributes.Normal)
            FileCounter = FileCounter + 1
        End If
    Next

    For Each DirectoryName In Directories
        If (IO.File.GetAttributes(DirectoryName) And IO.FileAttributes.Hidden) = IO.FileAttributes.Hidden Then
            'Show the folder.
            IO.File.SetAttributes(DirectoryName, IO.FileAttributes.Normal)
            FolderCounter = FolderCounter + 1
        End If
    Next
End Sub