如何搜索硬盘驱动器的文件类型。 [英] How do I search hard drive for file type.

查看:94
本文介绍了如何搜索硬盘驱动器的文件类型。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用vb2012按类型搜索所有驱动器的文件。我的代码正在工作

foe可移动驱动器,但是在搜索C:驱动器时,我在运行时遇到UnauthorizedAccessException错误。这就是我所拥有的:



I'm trying to use vb2012 to search all drives for files by type. The code I have is working
foe removable drive, but I get an error at run time for "UnauthorizedAccessException" when searching C: drive. Here is what I have:

Dim selDrive As Object
Dim drvLetter As String

For Each selDrive In chkdLstBoxDrives.CheckedItems

    drvLetter = selDrive.ToString
    drvLetter = Microsoft.VisualBasic.Left(drvLetter, 3)

    chkdLstBoxDrives.Hide()
    lstBoxFiles.Show()

    Dim fileNames = My.Computer.FileSystem.GetFiles(drvLetter, FileIO.SearchOption.SearchAllSubDirectories, "*.mp3") <=== error occurs here

     For Each fileName As String In fileNames
        lstBoxFiles.Items.Add(fileName)

    Next

Next



任何人都可以帮我解决这个问题吗? TIA



Mongo


Can anyone help me get past this? TIA

Mongo

推荐答案

有些子目录没有搜索权限。您必须在 Try..Catch 块中单独搜索每个子目录,以便跳过那些您没有的目录,而不是使用来自root的SearchAllSubDirectories。权限。
There are subdirectories that you don't have permission to search. Rather than using SearchAllSubDirectories from the root, you will have to search each subdirectory individually in a Try..Catch block so that you can skip over those directories for which you do not have permission.


当然,根据给代码的权限(以及UAC权限提升),某些文件系统对象将无法访问。



你无能为力。安全就是安全。在最嵌套的循环中,您可以在try-catch块下执行循环体语句,并在catch块中忽略问题(跳过该项)或在无法访问的项上添加一些数据。



顺便说一下,这是一种非常罕见的情况,可以认为某些异常的阻塞传播是合理的。在大多数其他情况下,您不应该在本地处理异常,而是放手(意思是自由传播)。例外情况只应在一些战略选择的能力点处理。



-SA
Of course, depending on the permissions given to your code (and UAC privilege elevation), some file system objects won't be accessible.

There is nothing you can do about it. Security is security. In your the most nested loop, you can execute the loop body statement under try-catch block, and in the catch block, either ignore problem (skip the item) or add some data on the item which cannot be accessed.

By the way, this is one of the pretty rare cases when blocking propagation of some exception can be considered reasonable. In most other cases, you should never handle exception locally, but "let go" (means "freely propagate"). Exceptions should be handled only on some strategically chosen "points of competence".

—SA


我建议你阅读:了解Windows文件和注册表权限 [ ^ ]
I would suggest you to read this: Understanding Windows File And Registry Permissions[^]


这篇关于如何搜索硬盘驱动器的文件类型。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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