I'm trying to retrieve a list of files from a specific folder, which has plenty of sub folder and files. The problem is that some of this folders, cannot be accessed due to access denied.
This is my first solution to search for the files:
Code:
public void GetFiles(string dir)
{
foreach (string d in Directory.GetDirectories(dir))
{
foreach (string f in Directory.GetFiles(d))
{
//do something with the file
}
GetFiles(d);
}
}
Using this I got an exception unhandled, I had to add a Try...Catch statement:
Code:
public void GetFiles(string dir)
{
try
{
foreach (string d in Directory.GetDirectories(dir))
{
foreach (string f in Directory.GetFiles(d))
{
//do something with the file
}
GetFiles(d);
}
}
catch (Exception)
{
}
}
The problem with this is that as soon as I reach a access denied folder I will get into the catch and the recursive search will stop. I figured the best way to build this method is to check whether the folder is able to be read or not.
There is any way to check to see if the folder can be read? Also, if someone has a better solution for my problem it would be nice to check what you got in mind.