Click here to Skip to main content
15,889,403 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

VB
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

Can anyone help me get past this? TIA

Mongo
Posted
Updated 21-Feb-14 5:56am
v2

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.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 21-Feb-14 12:06pm    
My 5. I told the same, with some extra explanations. Please see Solution 3.
—SA
Maciej Los 21-Feb-14 12:12pm    
Agree!
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
 
Share this answer
 
v2
Comments
Mike Meinz 21-Feb-14 12:09pm    
My 5 - Back at ya!
Sergey Alexandrovich Kryukov 21-Feb-14 12:13pm    
Thank you, Mike.
(We don't do reciprocal support, do we? We just vote for really good answers, right?)
—SA
Mike Meinz 21-Feb-14 12:15pm    
Yours is an excellent answer!
Sergey Alexandrovich Kryukov 21-Feb-14 12:46pm    
Thank you very much.
—SA
Maciej Los 21-Feb-14 12:12pm    
+5
I would suggest you to read this: Understanding Windows File And Registry Permissions[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 21-Feb-14 12:05pm    
My 4. This is useful but a bit away from the solution of the problem. Please see Solution 2 and 3.
—SA
Maciej Los 21-Feb-14 12:12pm    
Ok, i accept it. The only differ is: i did not provide any information how to around with it ;)
Sergey Alexandrovich Kryukov 21-Feb-14 12:13pm    
Exactly.
Cheers,
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900