Introduction
Over the weekend, I decided to try and import image metadata using C# and the .NET Framework. Aside from “normal” file attributes such as date modified and size, the Windows Explorer (shell) provides extended file property information which can be quite valuable.
The challenge was how to extract this information, given that the .NET Framework has somewhat limited support for this type of extraction? Read on to find out how.
Extended File Property Data
When you right click on a file in Windows Explorer and select “Properties”, you’ll notice a tab called “Details”.
Likewise, if you change the folder view to “Details” view and right click on the column headings, you can select “More…” which allows you to include extended property columns which are available to view.
Note that many properties will not be applicable depending on the type of files contained within the folder.
Once added, the Explorer view will show you additional property data for each file; this is the kind of data we’ll be querying using the .NET Framework.
Image Data
In my specific scenario, I want access to extended properties which relate to photos, e.g. ISO Speed, F-stop, Focal Length, Dimensions and so forth. Each property has an identifier which can be used to retrieve the associated property data. You can do this from .NET for some information, as seen here:
Image image = new Bitmap(file); PropertyItem propItem = image.GetPropertyItem(36867); string dateTaken = r.Replace(Encoding.UTF8.GetString(propItem.Value), "-", 2); var dateTaken = DateTime.Parse(dateTaken);
Therefore you can obtain more information like this by using Win32 interop and invoking shell calls directly from C#/.NET, but this can be time consuming, not to mention laborious as you have to track down the correct Win32 API interfaces and property item IDs to use. Surely there’s an easier way?
Introducing the Windows API Code Pack
Thankfully, someone has already done the leg work for us, and it has culminated in the extremely useful Windows API Code Pack, the latest release being available via this link.
The Pack itself contains documentation, samples, pre-compiled binaries and the source code for the binaries.
Honestly, I found the documentation and samples to be a bit underwhelming, but the core assemblies are absolutely gold – once you figure out how to use them properly. The following is a screenshot of the Explorer Browser which ships as a sample with the Pack:
This could be one of the most disturbing things I’ve seen in ages
Retrieving Extended Property Data
One of the best features of the Pack must be the fact that someone has gone and mapped all the extended property values to strongly typed definitions, which saves us a lot of time and effort.
For example, given the fully qualified path and file name of a photo, we could retrieve extended file information (such as Camera Manufacturer and Camera Model) by using the following code:
ShellObject picture = ShellObject.FromParsingName(file); var camera = picture.Properties.GetProperty(SystemProperties.System.Photo.CameraModel); newItem.CameraModel = GetValue(camera, String.Empty, String.Empty); var company = picture.Properties.GetProperty(SystemProperties.System.Photo.CameraManufacturer); newItem.CameraMaker = GetValue(company, String.Empty, String.Empty);
The SystemProperties class defines an incredibly useful hierarchy of extended property identifiers which can be used in conjunction with shell objects, as demonstrated above.
Using the Pack
Based on the above, it was easy as pie to extract extended photo property data. I wrote a small utility to demonstrate how straight forward it is to extract the required data. You need to reference two assemblies from the pack – Microsoft.WindowsAPICodePack.dll and Microsoft.WindowsAPICodePack.Shell.dll.
Here’s a complete dump of the code I used:
using System; using Microsoft.WindowsAPICodePack.Shell.PropertySystem; using Microsoft.WindowsAPICodePack.Shell; using System.Diagnostics; namespace DataTools.FilePropertyExtract { class Program { private static string GetValue(IShellProperty value) { if (value == null || value.ValueAsObject == null) { return String.Empty; } return value.ValueAsObject.ToString(); } static void Main(string[] args) { if (args.Length != 1) return; string filename = args[0]; if (!System.IO.File.Exists(filename)) return; ShellObject picture = ShellObject.FromParsingName(filename); if (picture != null) { var camera = GetValue(picture.Properties.
GetProperty(SystemProperties.System.Photo.CameraManufacturer)); var cameraModel = GetValue(picture.Properties.
GetProperty(SystemProperties.System.Photo.CameraModel)); var formattedString = String.Format("File {0} has Manufacturer {1} and Model {2}",
filename, camera, cameraModel); Trace.WriteLine(formattedString); } } } }
Which results in the following, if you pass a fully qualified path and file name of a photo to extract data from:
Note that in this case, the file did not have a Camera Manufacturer so the property was empty (but not null).
Summary
There’s obviously a lot more to this Pack than an abstraction of the Windows Shell API. I might do some more exploration at a later time, but I felt this article might be handy for those out there who needed an easier solution for extracting extended file property data. Enjoy.
R
7 thoughts on “Extracting file metadata with C# and the .NET Framework”
Hi Rob, very helpful code, thank you. I would like to enumerate all the properties of a file but have been unable to either hit the right foreach, nor find any examples. Do you have any suggestions? Regards, Dale
Hi Dale,
Sorry for the delayed response.
The main problem I can see you’ll have is that there are quite a lot of potential file metadata types. Purely from a performance point of view, it’ll be far easier to query the extended data if you know the base type of the file (even if you judge it based off the file extension) than it is to sort of generically process the file.
Otherwise, another approach could be to just query a pool of extended properties for each file, and drill down where you need to for specific types of files.
Cheers,
Rob
Hi, this sample has worked perfectly for me. Thank you. I do have one question however, I can return all metadata values except for the Latitude and Longitude values.
string longitude = GetValue(picture.Properties.GetProperty(SystemProperties.System.GPS.Longitude));
Would you know why?
Hi Ray,
Can you see these values in the file property dialog? Which OS are you running on, out of curiosity? I suspect the flag value might be wrong.
Cheers,
Rob
Hi Rob, Yeah I can see the latitude and longitude values in the file properties dialog. Im running on Windows Server 2012.
I’ll take a look and see if I can repro. What sort of filetype are you running against? If you’d be able to email me some test data, that’d be great (see About Rob for info).
Very cool man. I was looking for something just like this. Thanks!