Path access denied - Error no 5

M

Mick

I'm new to VB.NET (and Windows progamming in general) and am writing a small
program which allows the user to select a directory with or without
subdirectories and perform a series of automatic actions through scripting
in Photoshop on the files in the directories. I am using
Directory.GetFiles(folderName) to get a list of the files in the
directories. All is working fine except when I choose a directory which
contains a folder where the path access is denied such as the root of a
partition which contains a System Volume Information folder. I have tried
various things to trap the error but the best I have been able to manage is
a Try Catch which stops the whole process with Path Access Denied error.

Is there some simple way I can prevent Directory.GetFiles from attempting to
operate on directories which have Access Denied to prevent this error
occurring. I've been wading through the vast amounts of documentation and
can't find a solution. I'd be really grateful for some help.
 
G

Gary Milton

Hi Mick,

I don't know what sort of directory itteration loop you are using, but the
following is an example that will trap 'permission denied' errors on the
call to GetFiles and just skip processing of files in directories for which
the user has no access permission. If an error other than 'permission
denied' occurs then this will simply be thrown in the normal way as an
unhandled error.

\\\
Dim strDirectory As String
Dim strFile As String
Dim strFiles() As String
Dim blnProcessFiles As Boolean

For Each strDirectory In Directory.GetDirectories("C:\")
blnProcessFiles = True

Try
strFiles = Directory.GetFiles(strDirectory)

Catch ex As Exception
If (Err.Number = 5) Then 'Permission Denied
blnProcessFiles = False
Else
Throw New System.Exception(ex.Message)
End If
End Try

If blnProcessFiles Then
For Each strFile In strFiles
Console.WriteLine(strFile)
Next
End If
Next
///

Hope this helps.

Gary
 
M

Mick

Thanks for that Gary. I'll give it a try tomorrow night as it's too late
here to start on it again now. I'm calling the function recursively using a
for next loop and it should keep going until all directories are processed
but the whole thing stops on the exception. I may need to rethink how it's
done. I'm boggled by the vastness of VB.NET.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top