2014-01-26 102 views
1

我的代碼如下,以取消隱藏所有隱藏的文件,文件夾,子文件夾和子文件。問題是,如果任何訪問被拒絕,路徑會停止。如何使代碼跳過訪問被拒絕的路徑並繼續其他路徑。使用Visual Basic取消隱藏除訪問被拒絕路徑之外的所有隱藏文件,文件夾,子文件夾和子文件

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 
+2

直視的try/catch http://msdn.microsoft.com/en-us/library/fk6t46tz.aspx – Plutonix

回答

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 
+1

+1的解決方案,但是這將是一個更好的主意,提什麼類型的異常爲了可讀性並確保沒有其他問題。 – Buddha