Advertisement
Advertisement
| 04.09.2008 at 11:25AM PDT, ID: 23309188 |
|
[x]
Attachment Details
|
||
|
[x]
The Solution Rating System
|
||
With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.
Your Input Matters If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support. Thank you! |
||
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: |
Public Class RecursiveFileSearch
Shared log As New System.Collections.Specialized.StringCollection()
Public Shared Sub StartSearch()
' Start with drives if you have to search the entire computer.
Dim drives As String() = System.Environment.GetLogicalDrives()
For Each dr As String In drives
Dim di As New System.IO.DriveInfo(dr)
' Here we skip the drive if it is not ready to be read. This
' is not necessarily the appropriate action in all scenarios.
If Not di.IsReady Then
Console.WriteLine("The drive {0} could not be read", di.Name)
Continue For
End If
Dim rootDir As System.IO.DirectoryInfo = di.RootDirectory
WalkDirectoryTree(rootDir)
Next
' Write out all the files that could not be processed.
Console.WriteLine("Files with restricted access:")
For Each s As String In log
Console.WriteLine(s)
Next
' Keep the console window open in debug mode.
Console.WriteLine("Press any key")
Console.ReadKey()
End Sub
Private Shared Sub WalkDirectoryTree(ByVal root As System.IO.DirectoryInfo)
Dim files As System.IO.FileInfo() = Nothing
Dim subDirs As System.IO.DirectoryInfo() = Nothing
' First, process all the files directly under this folder
Try
files = root.GetFiles("*.*")
Catch e As UnauthorizedAccessException
' This is thrown if even one of the files requires permissions greater
' than the application provides.
' This code just writes out the message and continues to recurse.
' You may decide to do something different here. For example, you
' can try to elevate your privileges and access the file again.
log.Add(e.Message)
Catch e As System.IO.DirectoryNotFoundException
Console.WriteLine(e.Message)
End Try
If files IsNot Nothing Then
For Each fi As System.IO.FileInfo In files
' In this example, we only access the existing FileInfo object. If we
' want to open, delete or modify the file, then
' a try-catch block is required here to handle the case
' where the file has been deleted since the call to TraverseTree().
Console.WriteLine(fi.FullName)
Next
' Now find all the subdirectories under this directory.
subDirs = root.GetDirectories()
For Each dirInfo As System.IO.DirectoryInfo In subDirs
' Resursive call for each subdirectory.
WalkDirectoryTree(dirInfo)
Next
End If
End Sub
End Class
|