Исключение: отсутствие доступа - C#
Формулировка задачи:
Мне необходимо найти все картинки на флешке
Но на флешке есть папка System Volume Information, естественно исключение
Пробовал все обернуть в оберткуВсе равно программа вылетает. Как сделать так, что если нет доступа программа продолжила свою работу?
static void Main(string[] args)
{
string[] allFoundFiles;
allFoundFiles = Directory.GetFiles(@"h:", "*.jpg", SearchOption.AllDirectories);
foreach (string s in allFoundFiles)
{
Console.WriteLine(s);
}
Console.ReadKey();
} try
{
}
catch (Exception)
{
throw;
}Решение задачи: «Исключение: отсутствие доступа»
textual
Листинг программы
static void Main(string[] args)
{
var allFoundFiles = GetAllFiles(@"h:", "*.jpg");
foreach (string s in allFoundFiles)
{
Console.WriteLine(s);
}
Console.ReadKey();
}
static IEnumerable<string> GetAllFiles(string path, string searchPattern)
{
return Directory.EnumerateFiles(path, searchPattern).Concat(
Directory.EnumerateDirectories(path).SelectMany(d =>
{
try
{
return GetAllFiles(d, searchPattern);
}
catch (UnauthorizedAccessException)
{
return Enumerable.Empty<string>();
}
}));