I am making a console application and from a particular folder I want to get only the images with specific extension.
Below code I am trying but it's retrieving all the files from the particular path.
string[] extensions = { ".jpg", ".jpeg", ".png", ".gif", ".tif" };
FileInfo[] files = new DirectoryInfo(SourcePath).GetFiles("*.*", SearchOption.AllDirectories);
How can I set extension on FileInfo[]
?
Updated Issue
Answer
List<String> ImageFiles = Directory.GetFiles(SourcePath, "*.*",
SearchOption.AllDirectories)
.Where(file => new string[] { ".jpg", ".jpeg", ".png", ".gif", ".tif" }
.Contains(Path.GetExtension(file)))
.ToList();
List<FileInfo> files = new List<FileInfo>();
foreach (string filess in ImageFiles)
{
string replace = filess.Replace(@"\", "/");
files.Add(new FileInfo(replace.Split('/').Last()));
}
here how Can I get rid from for each loop as I am only needing file name and not the whole path