FileSystem.GetFiles()+ UnauthorizedAccessException错误? [英] FileSystem.GetFiles() + UnauthorizedAccessException error?

查看:297
本文介绍了FileSystem.GetFiles()+ UnauthorizedAccessException错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好像是 FileSystem.GetFiles()无法从UnauthorizedAccessException异常中恢复.net在尝试访问非限制目录时触发。



在这种情况下,这是否意味着这个类/方法是扫描整个驱动器没有用,我应该使用一些其他的解决方案(在这种情况下:哪一个?)?

这是一些代码来显示问题:

pre $私人小组bgrLongProcess_DoWork(BYVAL发件人作为System.Object,BYVAL e作为System.ComponentModel.DoWorkEventArgs)处理bgrLongProcess.DoWork
Dim drive As DriveInfo
Dim filelist As Collections.ObjectModel.ReadOnlyCollection(Of String)
Dim filePath As String

'扫描所有固定驱动器对于MyFiles。*
对于每个驱动器在DriveInfo.GetDrives()
如果drive.DriveType = DriveType .Fixed Then
尝试
'如何处理'访问路径'C:\ System Volume Information'被拒绝。错误?
filelist = My.Computer.FileSystem.GetFiles(drive.ToString,FileIO.SearchOption.SearchAllSubDirectories,MyFiles。*)
对于每个文件路径在文件列表中
DataGridView1.Rows.Add(文件路径.ToString,temp)
'触发ProgressChanged()事件
bgrLongProcess.ReportProgress(0,filepath)
下一个文件路径
Catch Ex As UnauthorizedAccessException
'忽略这个目录并继续前进?
End Try
End If
Next drive
End Sub
$ / code>编辑:如果使用Try / Catch来让GetFiles()填充数组,那么如何使用Try / Catch呢?

,忽略异常,只是恢复?

  Private Sub bgrLongProcess_DoWork(ByVal sender As System.Object,ByVal e As System.ComponentModel。 DoWorkEventArgs)处理bgrLongProcess.DoWork 
'在这里做冗长的东西
Dim filelist As Collections.ObjectModel.ReadOnlyCollection(Of String)
Dim filePath As String

filelist = Nothing
尝试
filelist = My.Computer.FileSystem.GetFiles(C:\,FileIO.SearchOption.SearchAllSubDirectories,MyFiles。*)
Catch ex As UnauthorizedAccessException
'如何忽略这个关闭的目录并恢复搜索?
结束尝试

对象引用未设置为对象的实例
对于每个文件路径在文件列表中
bgrLongProcess.ReportProgress(0,filepath)
Next filepath
End Sub


解决方案

在你的中的catch语句对filelist中的每个filepate 循环。因为现在当你捕获 UnauthorizedAccessException 时,你可以跳过剩下的 filelist 项目。



编辑

你是对的。当引发异常时,try-catch代价很高,通常你想在引发异常之前尝试检测这种情况。在这种情况下做这件事的一种方法是在做任何事情之前检查文件的访问权。



对于目录,有此GetAccessControl 函数。有是一个类似的功能可能需要中断GetFiles函数才能初始化目录,然后循环遍历每个目录,始终调用 GetAccessControl 为每个目录和文件。

It seems like FileSystem.GetFiles() is unable to recover from the UnauthorizedAccessException exception that .Net triggers when trying to access an off-limit directory.

In this case, does it mean this class/method isn't useful when scanning a whole drive and I should use some other solution (in which case: Which one?)?

Here's some code to show the issue:

Private Sub bgrLongProcess_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgrLongProcess.DoWork
    Dim drive As DriveInfo
    Dim filelist As Collections.ObjectModel.ReadOnlyCollection(Of String)
    Dim filepath As String

    'Scan all fixed-drives for MyFiles.*
    For Each drive In DriveInfo.GetDrives()
        If drive.DriveType = DriveType.Fixed Then
            Try
                'How to handle "Access to the path 'C:\System Volume Information' is denied." error?
                filelist = My.Computer.FileSystem.GetFiles(drive.ToString, FileIO.SearchOption.SearchAllSubDirectories, "MyFiles.*")
                For Each filepath In filelist
                  DataGridView1.Rows.Add(filepath.ToString, "temp")
                  'Trigger ProgressChanged() event
                  bgrLongProcess.ReportProgress(0, filepath)
                Next filepath
            Catch Ex As UnauthorizedAccessException
                'How to ignore this directory and move on?
            End Try
        End If
    Next drive
End Sub

Thank you.


Edit: What about using a Try/Catch just to have GetFiles() fill the array, ignore the exception and just resume?

Private Sub bgrLongProcess_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgrLongProcess.DoWork
    'Do lengthy stuff here
    Dim filelist As Collections.ObjectModel.ReadOnlyCollection(Of String)
    Dim filepath As String

    filelist = Nothing
    Try
        filelist = My.Computer.FileSystem.GetFiles("C:\", FileIO.SearchOption.SearchAllSubDirectories, "MyFiles.*")
    Catch ex As UnauthorizedAccessException
        'How to just ignore this off-limit directory and resume searching?
    End Try

    'Object reference not set to an instance of an object
    For Each filepath In filelist
        bgrLongProcess.ReportProgress(0, filepath)
    Next filepath
End Sub

解决方案

Put your try catch statement within your For each filepate in filelist loop. Because right now, when you catch UnauthorizedAccessException, you skip out on the rest of the filelist items.

Edit

You are right. A try-catch is expensive when an exception is thrown, and usually you want to try and detect that situation before the exception is thrown. One way to do that in this situation, is to check for Access to the file, before doing anything with it.

For directories, there is this GetAccessControl function. There is a similar function for Files.

You may have to break your GetFiles function to only get Directories initially, then loop through each directory recursively always calling GetAccessControl for each directory and file.

这篇关于FileSystem.GetFiles()+ UnauthorizedAccessException错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆