Vista: Getfiles throws security error
Greetings,
I wrote a multi-threaded program that allows a user to click on a folder and then get all pdf files in the folder and all subfolders under that folder. I then loop through the pdf files and get the number of pages each has.
It works fine except, on Vista machines. If I click on a user folder in My Documents, I get an exception that says "Access to path c:\Users\Username\Templates" is denied.
I can't elevate the request execution level because the program uses Click Once.
I need to be able to loop through all folders, including the special ones.
If I can't do that, then I would like the program to continue processing the rest of the folders that it is allowed to access. Then I can create an error log with the names of the folders that refuse access.
I have included a simplified version of the code below:
'This sub fires when the user clicks the Start button. It puts their path into the variable strPath
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' First create a FolderBrowserDialog object
Dim FolderBrowserDialog1 As New FolderBrowserDialog
' Then use the following code to create the Dialog window
' Change the .SelectedPath property to the default location
With FolderBrowserDialog1
.ShowNewFolderButton = False
.RootFolder = Environment.SpecialFolder.Desktop
' Prompt the user with a custom message."
.Description = "Select the source directory"
If .ShowDialog = DialogResult.OK Then
strpath = .SelectedPath
ProcessAllDirectories(strpath, objArrayList) ' These parameters are global
End If
End With
End Sub
ProcessAllDirectories(strpath, objArrayList)
Public Sub ProcessAllDirectories(ByVal strPath As String, ByRef objArrayList As ArrayList)
objDirectoryInfo = New DirectoryInfo(strPath)
t = New Threading.Thread(AddressOf ProcessDirectory)
t.IsBackground = True
t.Start()
End Sub
Public Sub ProcessDirectory()
Dim objFiles As FileInfo()
Dim objFileInfo As FileInfo
If objDirectoryInfo.GetFiles().Length > 0 Then
objFiles = objDirectoryInfo.GetFiles("*.pdf", SearchOption.AllDirectories) ' This line throws the exception
For Each objFileInfo In objFiles
'Do processing and counting here
Next
End If
End Sub
|