Reputation: 538
Is there a way to get files by generic mime types?
Example: For all the images ("image/png, image/jpeg, ..."):
DirectoryInfo.GetFiles("image")
Upvotes: 0
Views: 429
Reputation: 10044
For compatibility reasons, you're best off including the file types yourself in an array, eg.
string[] imagemime = new []{".jpg", ".gif", ".png", ".bmp", ".jpe", ".jpeg", ".wmf", ".emf", ".xbm", ".ico", ".eps", ".tif", ".tiff", ".g01", ".g02", ".g03", ".g04", ".g05", ".g06", ".g07", ".g08"};
Then you could have a method like this:
private static IEnumerable<string> GetFiles(string sourceFolder, string[] exts, System.IO.SearchOption searchOption)
{
return System.IO.Directory.GetFiles(sourceFolder, "*.*", searchOption)
.Where(s=>exts.Contains(System.IO.Path.GetExtension(s), StringComparer.OrdinalIgnoreCase));
}
You can see a comparison of the performance with other alternatives here.
Upvotes: 2
Reputation: 11201
The DirectoryInfo.GetFiles Method allows to put searchpattern of with wild character such as ? and *
So the DirectoryInfo.GetFiles does not have any options to get files by mime type
But you can use UrlMon for it look at this post for example : Using .NET, how can you find the mime type of a file based on the file signature not the extension
Upvotes: 0
Reputation: 2394
Not easily. DirectoryInfo goes against the file system, which doesn't inherently contain MIME type information.
You could get all the files by extension using GetFiles() and the extension. You could also hit HKEY_CLASSES_ROOT\MIME\Database\Content Type
registry key to turn a list of MIME types into a list of extensions.
Upvotes: 1
Reputation: 1366
I'm not sure, but do you mean Directory.GetFiles? If so, you can search for specific file formats like:
Directory.GetFiles(filepath, ".png");
Directory.GetFiles(filepath, ".jpeg");
// etc...
The extensions would be a clear give away of the MIME type.
Upvotes: 0