How to skip folders with unauthorized access while creating fileinfo list? [duplicate]

一笑奈何 提交于 2019-12-20 03:27:11

问题


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
{
}

回答1:


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)



来源:https://stackoverflow.com/questions/19137099/how-to-skip-folders-with-unauthorized-access-while-creating-fileinfo-list

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!