扫描 C: 中的文件时忽略错误

发布于 2024-09-04 17:08:25 字数 692 浏览 4 评论 0原文

我正在尝试在 C:\ 驱动器中搜索具有特定扩展名的所有文件。我正在使用以下代码,该代码工作正常,但是当遇到错误时,整个过程会停止,而不是继续扫描。 (在后台工作程序中运行,因此调用)

Private Sub ScanFiles(ByVal rootFolder As String, ByVal fileExtension As String)

        'Determine if the current folder contains any sub folders
        Dim subFolders() As String = System.IO.Directory.GetDirectories(rootFolder)

        For Each subFolder As String In subFolders
            ScanFiles(subFolder, fileExtension)
        Next
            For Each file As String In System.IO.Directory.GetFiles(rootFolder, fileExtension)
                lb.BeginInvoke(New AddValue(AddressOf AddItems), file)
            Next

    End Sub

一旦遇到错误,如何使此代码继续运行?

I am trying to search the C:\ drive for all files with a certain extension. I am using the following code which is working fine, however when it encounters an error the whole process stops rather than continuing with the scan. (running in backgroundworker, hence the invoke)

Private Sub ScanFiles(ByVal rootFolder As String, ByVal fileExtension As String)

        'Determine if the current folder contains any sub folders
        Dim subFolders() As String = System.IO.Directory.GetDirectories(rootFolder)

        For Each subFolder As String In subFolders
            ScanFiles(subFolder, fileExtension)
        Next
            For Each file As String In System.IO.Directory.GetFiles(rootFolder, fileExtension)
                lb.BeginInvoke(New AddValue(AddressOf AddItems), file)
            Next

    End Sub

How can I make this code continue once an error is encountered?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

久隐师 2024-09-11 17:08:25

如果您无权探索 C 驱动器本身,那么您就不走运了。但是,如果您因为无权访问树中的某些子文件夹而遇到异常,则可以通过将代码放入 try-catch 块来避免它。

Private Sub ScanFiles(ByVal rootFolder As String, ByVal fileExtension As String)
   'Determine if the current folder contains any sub folders    '
   try

        Dim subFolders() As String = System.IO.Directory.GetDirectories(rootFolder)

        For Each subFolder As String In subFolders
            ScanFiles(subFolder, fileExtension)
        Next
            For Each file As String In System.IO.Directory.GetFiles(rootFolder, fileExtension)
                lb.BeginInvoke(New AddValue(AddressOf AddItems), file)
            Next
    catch (Ex As UnauthorizedAccessException)
       'Ignore Access Errors   '
    end try
End Sub

If you don't have access to explore C drive itself then you are out of luck. but if you are getting an exception because you don't have access to some child folder in the tree, you can avoid it by putting your code in an try-catch block.

Private Sub ScanFiles(ByVal rootFolder As String, ByVal fileExtension As String)
   'Determine if the current folder contains any sub folders    '
   try

        Dim subFolders() As String = System.IO.Directory.GetDirectories(rootFolder)

        For Each subFolder As String In subFolders
            ScanFiles(subFolder, fileExtension)
        Next
            For Each file As String In System.IO.Directory.GetFiles(rootFolder, fileExtension)
                lb.BeginInvoke(New AddValue(AddressOf AddItems), file)
            Next
    catch (Ex As UnauthorizedAccessException)
       'Ignore Access Errors   '
    end try
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文