I am trying to get all files from folder this way:
try
{
string[] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath, "*.*", SearchOption.AllDirectories);
}
catch (UnauthorizedAccessException)
{
throw;
}
If my root folder contains a folder for which the user doesn’t have permission to access, the UnauthorizedAccessException is caught and my array is empty and all the recursion failed.
How can I handle this case and insure that my code ignore locations without permission but add the files from location with permissions?
see SafeFileEnumerator on this other post. I have used the SafeFileEnumerator code in the past with success. It prevents loosing the entire enumeration when you simply don’t have access to a single file so you are still able to iterate through the files that you can access.
EDIT: The version I have is slightly different from the one I linked to so let me share the version I have.
Example Usage: