Question Why does this coding fail if drive directory chosen?

tommyt11

Member
Joined
Dec 21, 2015
Messages
23
Location
Columbus, Ohio
Programming Experience
5-10
Hi, I'm confused. This code works fine as long as directory chosen from directory dialog is not a root directory on any drive. Example if I choose C:/ or F:/ it fails and I get exception:
'System.UnauthorizedAccessException' occurred in mscorlib.dll.
Is there a way to handle this exception and continue the process on folders not readonly or set permissions so I can.

If I choose a subdirectory. example F:/Something it works as I assume there is no permissions problems at the lower level.
Here is code:
Public Sub GetFilesForTransfer()

On Error Resume Next
Dim SelectedsearchPath = SearchPathTextBox.Text
Dim searchMask = MaskComboBoxMain.Text
If SubFoldersYes.Checked Then
For Each foundFile As String In My.Computer.FileSystem.GetFiles(SelectedsearchPath, FileIO.SearchOption.SearchAllSubDirectories, searchMask)
SearchResultsListBox.Items.Add(foundFile)
Next
Else

For Each foundFile As String In My.Computer.FileSystem.GetFiles(SelectedsearchPath, FileIO.SearchOption.SearchTopLevelOnly, searchMask)
SearchResultsListBox.Items.Add(foundFile)
Next
End If

On Error Resume Next

End Sub
 
The GetFiles method will fail if you specify SearchAllSubDirectories and the folder you specify contains an inaccessible subfolder somewhere, which will definitely be the case on a system drive at least. If you want to be able to handle that scenario then you'll have to write your own recursive file search.
 
Okay Let's say I just want to prohibit user from this scenario and I go this route,If
SearchPathTextBox.Text = ""

but display a prompt that this can not be done.
how do I wild card Search text so they can't use any root directory like
SearchPathTextBox.Text = "?:" which of course doesn't work.
I can do an OR but it's tedious, there has to be a way to wild card this I would think>





 
But how would I handle that simply with out a boatload of if's. I was trying to avoid a long string of if or's. Maybe I'm not understanding. Is there an example of what you're talking about .
 
Back
Top