1
Private Function MasterDirectoryScanner(ByVal DirectroyInfos As DirectoryInfo, ByVal filter As String) As List(Of UInt64)
    Dim FileInformation As New List(Of UInt64)

    Dim file_size As UInt64 = 0
    Dim file_count As Integer = 0

   **For Each fi In DirectroyInfos.GetFiles(filter)**
        Try
            file_count = file_count + 1
            file_size = CULng(file_size + fi.Length)
            FilesToDelete.Add(fi.FullName)
        Catch ex As UnauthorizedAccessException
            'There's really no pretty way to handle this exception
        Catch ex As FileNotFoundException
            'There's really no pretty way to handle this exception
        End Try
    Next

    For Each di As DirectoryInfo In DirectroyInfos.GetDirectories
        MasterDirectoryScanner(di, filter)
    Next

    FileInformation.Add(CULng(file_count))
    FileInformation.Add(file_size)

    Return FileInformation
End Function

我在这行“For Each fi In DirectroyInfos.GetFiles(filter)”中遇到错误

System.UnauthorizedAccessException was unhandled
Message=Access to the path 'C:\Windows\CSC' is denied.
Source=mscorlib
StackTrace:
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.Directory.InternalGetFileDirectoryNames(String path, String userPathOriginal, String searchPattern, Boolean includeFiles, Boolean includeDirs, SearchOption searchOption)
   at System.IO.DirectoryInfo.GetFiles(String searchPattern, SearchOption searchOption)
   at System.IO.DirectoryInfo.GetFiles(String searchPattern)
   at ACleaner.UniducksCleaner.MasterDirectoryScanner(DirectoryInfo DirectroyInfos, String filter) in C:\Users\Sunny\Desktop\Developer Kit\ACleaner\Classes\UniducksCleaner.vb:line 272
   at ACleaner.UniducksCleaner.MasterDirectoryScanner(DirectoryInfo DirectroyInfos, String filter) in C:\Users\Sunny\Desktop\Developer Kit\ACleaner\Classes\UniducksCleaner.vb:line 285
   at ACleaner.UniducksCleaner.MasterScanner(String FileRegOrDirectory, ListView lvw, DirectoryInfoA di, FileInfoA fi, RegistryInfoA ri) in C:\Users\Sunny\Desktop\Developer Kit\ACleaner\Classes\UniducksCleaner.vb:line 324
   at ACleaner.UniducksCleaner.scan_Directories(ListView lvw) in C:\Users\Sunny\Desktop\Developer Kit\ACleaner\Classes\UniducksCleaner.vb:line 237
   at ACleaner.UniducksCleaner.Scan(ListView lvw) in C:\Users\Sunny\Desktop\Developer Kit\ACleaner\Classes\UniducksCleaner.vb:line 218
   at ACleaner.frmMain.Initiate() in C:\Users\Sunny\Desktop\Developer Kit\ACleaner\frmMain.vb:line 533
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.runTryCode(Object userData)
   at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

内部异常:

4

1 回答 1

1

The Try-Catch does not handle the error because the error occurs outside the try-catch block.

You can do one or both of these to prevent the crash:

  1. Include the initial for statement in the try-catch block, and

  2. Check the permissions of DirectroyInfos. Here's how:

    If ((File.GetAttributes(foldername) And FileAttributes.System) = FileAttributes.System) then ...

于 2013-09-06T00:17:35.990 回答