Why does this coding fail if drive directory chosen?
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:
Dim SelectedsearchPath = SearchPathTextBox.Text
Dim searchMask = MaskComboBoxMain.Text
If SubFoldersYes.Checked Then
ForEach foundFile AsStringInMy.Computer.FileSystem.GetFiles(SelectedsearchPath, FileIO.SearchOption.SearchAllSubDirectories, searchMask)
ForEach foundFile AsStringInMy.Computer.FileSystem.GetFiles(SelectedsearchPath, FileIO.SearchOption.SearchTopLevelOnly, searchMask)
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.
OK Thanks for the response, I can do that.
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>
You don't need to use a wildcard. The Path class can give you the root path from another path and you can then just test whether they are the same.
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 .
You need one If: if the path entered is equal to the root of the path entered.
Okay, now I get it. Thanks