在VB.net中获取错误“System.UnauthorizedAccessException:访问路径”

时间:2013-09-05 19:36:14

标签: vb.net

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()

InnerException:

1 个答案:

答案 0 :(得分:1)

Try-Catch不处理错误,因为错误发生在try-catch块之外。

您可以执行其中一项或两项操作以防止崩溃:

  1. 在try-catch块中包含初始for语句,

  2. 检查DirectroyInfos的权限。方法如下:

    如果((File.GetAttributes(foldername)和FileAttributes.System)= FileAttributes.System)那么......