2

I have this for finding files and listing to list .but when it comes to a folder that needs authorized access, it stops. How can I make this skip those folders and carry on?

string[] filetypes = new string[] { "3gp", "avi", "dat", "mp4", "wmv", 
                                                         "mov", "mpg", "flv",  }
try
{
    foreach (string ft in filetypes)
    {                    
        files.AddRange(dif.GetFiles(string.Format("*.{0}", ft),
                                                  SearchOption.AllDirectories));    
    }
}
catch
{
}
Kamil Budziewski
  • 22,699
  • 14
  • 85
  • 105
emmett
  • 193
  • 2
  • 14

1 Answers1

4
static void GetFiles(string dir)
{
    string[] filetypes = new string[] { "3gp", "avi", "dat", "mp4", "wmv", 
                                                     "mov", "mpg", "flv",  }
    foreach(string ft in filetypes)
    {
       foreach (string file in Directory.GetFiles(dir, string.Format("*.{0}", ft),
                                              SearchOption.TopDirectoryOnly)))
       { 
             files.Add(new FileInfo(file));
       }
    }
    foreach (string subDir in Directory.GetDirectories(dir))
    {
        try
        {
            GetFiles(subDir);
        }
        catch
        {
        }
    }
}

Do it using recursion not AllDirectories

run it like GetFiles(dif)

Kamil Budziewski
  • 22,699
  • 14
  • 85
  • 105
  • -1 This is going to fail if any of the child directory fails. You should note `SearchOption.AllDirectories` – Sriram Sakthivel Oct 02 '13 at 12:47
  • sorry for my ignorance but since "file" is List files = new List(); I cant add string into it. And this process will be triggeret by a button like button1_click. Can you rearrange it please – emmett Oct 02 '13 at 14:59
  • @emmett you can use FileInfo constructor accepting string path, I edited my answer. What's wrong with calling `GetFiles(dir)` in button1_click handler? – Kamil Budziewski Oct 02 '13 at 16:07
  • @wudzik Thanks. I got it now. but I had to change static void to public. Can you explain the difference shortly like in one sentence? lastly if wanted to textbox which shows currenl searching dir, where would I put textbox1.text=dir ? I put after first foreach but itdoenst seem to work – emmett Oct 03 '13 at 00:22
  • @emmett read about access modifiers http://msdn.microsoft.com/en-us/library/wxh6fsc7.aspx. You need to put it right after first bracket, before `string[] filetypes`. – Kamil Budziewski Oct 03 '13 at 05:36