我有这个用于查找文件和列表列表。但是当涉及到需要授权访问的文件夹时,它会停止。如何跳过这些文件夹并继续?
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
{
}
答案 0 :(得分: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
{
}
}
}
使用递归而不是AllDirectories
像GetFiles(dif)