public void SearchFile(string FileLocation)
{
// Расширения изображений
var Extensions = new[]
{
".jpg", ".png"
};
string[] files = Directory.GetFiles(FileLocation);
string[] Directories = Directory.GetDirectories(FileLocation);
for (int i = 0; i < files.Length; i++)
{
string extension = Path.GetExtension(files[i]);
if (Extensions.Contains(extension))
{
// обрабатываем файл files[i]
}
}
for (int i = 0; i < Directories.Length; i++)
{
SearchFile(Directories[i]);
}
}
class Program
{
static readonly string[] Extensions = new string[] { "*.jpg", "*.png" };
static void Main(string[] args)
{
var drives = DriveInfo.GetDrives().Where(d => d.DriveType == DriveType.Fixed);
drives.AsParallel().ForAll(d =>
{
SearchFile(d.RootDirectory.FullName);
});
}
static void SearchFile(string path, int maxDegree = 1)
{
var images = Directory.GetFiles(path).Where(f =>
{
var ext = Path.GetExtension(f);
return Extensions.Any(e => e.Equals(ext, StringComparison.OrdinalIgnoreCase));
});
images.ToList().ForEach(i =>
{
// process image
});
var dirs = Directory.GetDirectories(path);
dirs.AsParallel().WithDegreeOfParallelism(maxDegree).ForAll(d => SearchFile(d));
}
}