Extracting File Metadata with C# and the .NET Framework
Join the DZone community and get the full member experience.
Join For FreeIntroduction
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 usefulWindows 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 andMicrosoft.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.
Published at DZone with permission of Rob Sanders, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments