Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is there an easy way to ignore files, I don't have access to?
I get sometimes an UnauthorizedAccessException for files. If I handle it, my method will be quit.

Here's the code:
string[] files;
files = Directory.GetFiles("C:\MyDir", "*.*", SearchOption.AllDirectories);
Posted
Updated 25-Mar-11 1:15am
v2
Comments
Tarun.K.S 25-Mar-11 7:16am    
How about using the Try-Catch statement.
dj_jeff 25-Mar-11 9:47am    
I'll give Solution 3 a try.

Thanx a lot @all.

C#
try
{
    string[] files = Directory.GetFiles("C:\MyDir", "*.*", SearchOption.AllDirectories);
    // do something with your file array
}
catch (UnauthorizedAccessException)
{
    // this eats the exception  but it will still stop
}
catch (Exception ex)
{
    // you can handle all other exceptions here
}


GetFileSystemEntries will also throw the UnauthorizedAccessException, so I don't think you'll gain anything by using it instead. I also don't know the condition of the array of files - does it continue on with the operation or stop at the exception? I'd have to try running some code, but then again, you can do that yourself.
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 25-Mar-11 13:22pm    
The only correct answer, a 5.
--SA
willempipi 28-Mar-11 10:14am    
What if your first subdirectory is not accessible and the second is, will the second subdirectory be included in the files array ;) Answer: No, the array will always be empty because the "UnauthorizedAccessException" is thrown before you receive any list.

Now look again at my answer;) "The only correct answer" ;)
Use recursion rather than the SearchOption.AllDirectories value.

e.g.

http://www.dotnetperls.com/recursively-find-files[^]

Or MSDN version

http://msdn.microsoft.com/en-us/library/07wt70x2.aspx[^]

This way, you can just try-catch on any directories with permissions issues and continue on to the next directory.

See this on stack overflow as well, same issue as you

http://stackoverflow.com/questions/172544/ignore-folders-files-when-directory-getfiles-is-denied-access[^]
 
Share this answer
 
I believe the error occurs when you don't have sufficient rights, meaning you or the app is not allowed to browse it.

You could also try looking at this but it'll probably error too.
Directory.GetFileSystemEntries [^]
 
Share this answer
 
v3
private void Form1_Load(object sender, EventArgs e)
{
    DirectoryInfo dir_info = new DirectoryInfo("C:\\MyDir");
    List<string> file_list = new List<string>();
    SearchDirectory(dir_info, file_list);
}

private void SearchDirectory(DirectoryInfo dir_info, List<string> file_list)
{
    try
    {
        foreach (DirectoryInfo subdir_info in dir_info.GetDirectories())
        {
            SearchDirectory(subdir_info, file_list);
        }
    }
    catch
    {
    }
    try
    {
        foreach (FileInfo file_info in dir_info.GetFiles())
        {
            file_list.Add(file_info.FullName);
        }
    }
    catch
    {
    }
}


Here you go...
 
Share this answer
 
Comments
Member 15028314 29-Apr-21 19:27pm    
My comment, Hum?? I'm sorry, but seeing is believing. I believe I need to see this in action first!; before passing judgement. Okay?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900