tags:

views:

2775

answers:

4

I am trying to display a list of all files found in the selected directory (and optionally any subdirectories). The problem I am having is that when the GetFiles() method comes across a folder that it cannot access, it throws an exception and the process stops.

How do I ignore this exception (and ignore the protected folder/file) and continue adding accessible files to the list?

try
{
if (cbSubFolders.Checked == false)
{
string[] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
foreach (string fileName in files)
ProcessFile(fileName);
}
else
{
string[] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath, "*.*", SearchOption.AllDirectories);
foreach (string fileName in files)
ProcessFile(fileName);
}
lblNumberOfFilesDisplay.Enabled = true;
}
catch (UnauthorizedAccessException)
{}
finally {}
+4  A: 

You will have to do the recursion manually; don't use AllDirectories - look one folder at a time, then try getting the files from sub-dirs. Untested, but something like below (note uses a delegate rather than building an array):

using System;
using System.IO;
static class Program
{
    static void Main()
    {
        string path = ""; // TODO
        ApplyAllFiles(path, ProcessFile);
    }
    static void ProcessFile(string path) {/* ... */}
    static void ApplyAllFiles(string folder, Action<string> fileAction)
    {
        foreach (string file in Directory.GetFiles(folder))
        {
            fileAction(file);
        }
        foreach (string subDir in Directory.GetDirectories(folder))
        {
            try
            {
                ApplyAllFiles(subDir, fileAction);
            }
            catch
            {
                // swallow, log, whatever
            }
        }
    }
}
Marc Gravell
+2  A: 

This should answer the question. I've ignored the issue of going through subdirectories, I'm assuming you have that figured out.

Of course, you don't need to have a seperate method for this, but you might find it a useful place to also verify the path is valid, and deal with the other exceptions that you could encounter when calling GetFiles().

Hope this helps.

private string[] GetFiles(string path)
{
    string[] files = null;
    try
    {
       files = Directory.GetFiles(path);
    }
    catch (UnauthorizedAccessException)
    {
       // might be nice to log this, or something ...
    }

    return files;
}

private void Processor(string path, bool recursive)
{
    // leaving the recursive directory navigation out.
    string[] files = this.GetFiles(path);
    if (null != files)
    {
        foreach (string file in files)
        {
           this.Process(file);
        }
    }
    else
    {
       // again, might want to do something when you can't access the path?
    }
}
A: 

Well, i am still suffering through this error. please let me know too if anyone know the exect solution. because the above solution do not work. please see my code below

private void LoadParendDirectories() {

    String path = DropDownList1.SelectedValue;
    string[] strFileFullBackup = System.IO.Directory.GetDirectories(path);
    ListBox1.Items.Clear();
    for (int i = 0; i < strFileFullBackup.Length; i++)
    {            
        LoadChild(strFileFullBackup[i].ToString());
    }

    string[] strFiles = Directory.GetFiles(path + "\\", "*" + DropDownList2.Text);
    foreach (string _Files in strFiles)
    {
        ListBox1.Items.Add(new ListItem(_Files));
    }
}

private void LoadChild(string _DirectoryName) {

        string[] strCheckDir = Directory.GetDirectories(_DirectoryName);

        if (strCheckDir.Length < 1)
        {                
            string[] strFiles = Directory.GetFiles(_DirectoryName + "\\",  "*" + DropDownList2.Text );

            foreach (string _Files in strFiles)
            {
                ListBox1.Items.Add(new ListItem(_Files));
            }
        }
        else if (strCheckDir.Length > 0)
        {
            string[] strDirectories = Directory.GetDirectories(_DirectoryName + "\\");
            foreach (string _NestedDirectory1 in strDirectories)
            {
                LoadChild(_NestedDirectory1);
            }
        }

}

Many Regards,

Ali Mardan.

A: 

Also check, How to: Iterate Through a Directory Tree.