首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FileSystem.GetFiles() + UnauthorizedAccessException错误?

FileSystem.GetFiles() + UnauthorizedAccessException错误?
EN

Stack Overflow用户
提问于 2010-03-15 19:00:48
回答 3查看 7.2K关注 0票数 0

看起来像 FileSystem.GetFiles()无法从.Net试图访问限制目录时触发的UnauthorizedAccessException异常中恢复。

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

下面是一些显示问题的代码:

代码语言:javascript
复制
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

谢谢。

编辑:如果只使用Try/Catch来填充数组,忽略异常,然后继续执行呢?

代码语言:javascript
复制
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
EN

回答 3

Stack Overflow用户

发布于 2010-03-15 19:04:37

将try catch语句放入您的For each filepate in filelist循环中。因为现在,当您捕获UnauthorizedAccessException时,将跳过其余的filelist项。

编辑

你是正确的。当抛出异常时,尝试捕获是很昂贵的,通常您希望在抛出异常之前尝试并检测这种情况。在这种情况下,一种方法是检查对文件的访问,然后再对它做任何操作。

对于目录,有这个GetAccessControl函数。是一个类似的函数 for Files。

您可能必须先中断您的GetFiles函数才能获得目录,然后递归地遍历每个目录,始终为每个目录和文件调用GetAccessControl

票数 1
EN

Stack Overflow用户

发布于 2010-03-16 11:40:21

对于感兴趣的人,我找到了以下工作解决方案:

http://dotnetperls.com/recursive-file-directory-vbnet

如果你想过滤搜索到给定类型的文件(例如。"MyFiles.*"),更改

代码语言:javascript
复制
Public Shared Function GetFilesRecursive(ByVal initial As String, ByVal extension As String) As List(Of String)

..。所以你可以这样称呼它:

代码语言:javascript
复制
Dim list As List(Of String) = FileHelper.GetFilesRecursive("C:\", "MyFiles.*")
票数 0
EN

Stack Overflow用户

发布于 2015-12-15 14:31:27

我使用以下函数搜索系统上的所有驱动器,以查找特定的文件,并且当“GetFiles”按下请求时,它不会中断。这是通过使用“GetDirectories”执行顶级只搜索来完成的,这将为您提供一个基本级别的目录列表,如果它不抛出对该目录的所有子目录的“GetFiles”搜索,则如果命中“GetDirectories”目录,则“GetDirectories”目录将被定位,并且进程将继续。在由“GetFiles”执行的每一次成功搜索之后,包含所搜索文件的目录都存储在列表中。一旦搜索了系统上的所有现有驱动器,主函数将返回它添加到列表中的数据,从而结束它的操作。

代码语言:javascript
复制
Public Function Left(ByVal TextString As String, ByVal LocateString As String) As String
    Try
        Left = Microsoft.VisualBasic.Left(TextString, InStrRev(TextString, LocateString) - 1)
    Catch ex As Exception
        Left = TextString
    End Try
End Function

Public Function GetExistingDrives() As List(Of String)
    Dim LocatedDrives As New List(Of String), DriveInformation As System.IO.DriveInfo() = System.IO.DriveInfo.GetDrives
    For Each FoundDrive As System.IO.DriveInfo In DriveInformation
        Try
            LocatedDrives.Add(UCase(Left(FoundDrive.Name, "\")))
        Catch ex As Exception
        End Try
    Next
    GetExistingDrives = LocatedDrives
End Function

Public Function LocateFiles(ByVal ParamArray SearchPattern As String()) As List(Of String)
    Dim LocatedDirectoriesList As New List(Of String), LocatedFilenameList As System.Collections.ObjectModel.ReadOnlyCollection(Of String)
    For Each DriveLetter In GetExistingDrives()
        Try
            For Each SearchDirectory As String In My.Computer.FileSystem.GetDirectories(DriveLetter, FileIO.SearchOption.SearchTopLevelOnly)
                Try
                    LocatedFilenameList = My.Computer.FileSystem.GetFiles(SearchDirectory, FileIO.SearchOption.SearchAllSubDirectories, SearchPattern)
                Catch ex As Exception
                End Try
                If (LocatedFilenameList.Count <> 0) Then
                    For Each LocatedFilename As String In LocatedFilenameList
                        Dim LocatedDirectory As String = Left(LocatedFilename, "\")
                        If (LocatedDirectoriesList.IndexOf(LocatedDirectory) = -1) Then LocatedDirectoriesList.Add(LocatedDirectory)
                    Next
                End If
            Next
        Catch ex As Exception
        End Try
    Next
    LocateFiles = LocatedDirectoriesList
End Function
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2449700

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档