How do you filter on more than one extension?
I've tried:
FileInfo[] Files = dinfo.GetFiles("*.jpg;*.tiff;*.bmp");
FileInfo[] Files = dinfo.GetFiles("*.jpg,*.tiff,*.bmp");
How do you filter on more than one extension?
I've tried:
FileInfo[] Files = dinfo.GetFiles("*.jpg;*.tiff;*.bmp");
FileInfo[] Files = dinfo.GetFiles("*.jpg,*.tiff,*.bmp");
You can't do that, because GetFiles
only accepts a single search pattern. Instead, you can call GetFiles
with no pattern, and filter the results in code:
string[] extensions = new[] { ".jpg", ".tiff", ".bmp" };
FileInfo[] files =
dinfo.GetFiles()
.Where(f => extensions.Contains(f.Extension.ToLower()))
.ToArray();
If you're working with .NET 4, you can use the EnumerateFiles
method to avoid loading all FileInfo objects in memory at once:
string[] extensions = new[] { ".jpg", ".tiff", ".bmp" };
FileInfo[] files =
dinfo.EnumerateFiles()
.Where(f => extensions.Contains(f.Extension.ToLower()))
.ToArray();
The following retrieves the jpg, tiff and bmp files and gives you an IEnumerable<FileInfo>
over which you can iterate:
var files = dinfo.GetFiles("*.jpg")
.Concat(dinfo.GetFiles("*.tiff"))
.Concat(dinfo.GetFiles("*.bmp"));
If you really need an array, simply stick .ToArray()
at the end of this.
I know there is a more elegant way to do this and I'm open to suggestions... this is what I did:
try
{
// Set directory for list to be made of
DirectoryInfo jpegInfo = new DirectoryInfo(destinationFolder);
DirectoryInfo jpgInfo = new DirectoryInfo(destinationFolder);
DirectoryInfo gifInfo = new DirectoryInfo(destinationFolder);
DirectoryInfo tiffInfo = new DirectoryInfo(destinationFolder);
DirectoryInfo bmpInfo = new DirectoryInfo(destinationFolder);
// Set file type
FileInfo[] Jpegs = jpegInfo.GetFiles("*.jpeg");
FileInfo[] Jpgs = jpegInfo.GetFiles("*.jpg");
FileInfo[] Gifs = gifInfo.GetFiles("*.gif");
FileInfo[] Tiffs = gifInfo.GetFiles("*.tiff");
FileInfo[] Bmps = gifInfo.GetFiles("*.bmp");
// listBox1.Items.Add(@""); // Hack for the first list item no preview problem
// Iterate through each file, displaying only the name inside the listbox...
foreach (FileInfo file in Jpegs)
{
listBox1.Items.Add(file.Name);
Photo curPhoto = new Photo();
curPhoto.PhotoLocation = file.FullName;
metaData.AddPhoto(curPhoto);
}
foreach (FileInfo file in Jpgs)
{
listBox1.Items.Add(file.Name);
Photo curPhoto = new Photo();
curPhoto.PhotoLocation = file.FullName;
metaData.AddPhoto(curPhoto);
}
foreach (FileInfo file in Gifs)
{
listBox1.Items.Add(file.Name);
Photo curPhoto = new Photo();
curPhoto.PhotoLocation = file.FullName;
metaData.AddPhoto(curPhoto);
}
foreach (FileInfo file in Tiffs)
{
listBox1.Items.Add(file.Name);
Photo curPhoto = new Photo();
curPhoto.PhotoLocation = file.FullName;
metaData.AddPhoto(curPhoto);
}
foreach (FileInfo file in Bmps)
{
listBox1.Items.Add(file.Name);
Photo curPhoto = new Photo();
curPhoto.PhotoLocation = file.FullName;
metaData.AddPhoto(curPhoto);
}
Why not create an extension method? That's more readable.
public static IEnumerable<FileInfo> GetFilesByExtensions(this DirectoryInfo dir, params string[] extensions)
{
if (extensions == null)
throw new ArgumentNullException("extensions");
IEnumerable<FileInfo> files = Enumerable.Empty<FileInfo>();
foreach(string ext in extensions)
{
files = files.Concat(dir.GetFiles(ext));
}
return files;
}
Usage:
dInfo.GetFilesByExtensions(".jpg",".exe",".gif");