Something that should be in everyone's pack, the Windows API Code Pack
Today's Windows Wednesday project is one that's maybe not earth shattering or beyond wild, but could end up helping more of you than not. Helping you take advantage of the Windows OS in a much simpler, easier and Managed friendly way
Let's say you're building an app that needs access to file metadata, maybe picture metadata? Location, F-Stop, Camera? Or music metadata? Video?
Rob Sanders shows us how...
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.
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
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
The Pack itself contains documentation, samples, pre-compiled binaries and the source code for the binaries.
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:
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:..
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.
Windows® API Code Pack for Microsoft® .NET Framework provides a source code library that can be used to access some features of Windows 7 and Windows Vista from managed code. These Windows features are not available to developers today in the .NET Framework.
The individual features supported in this version (v1.1) of the library are:
What’s New in this update (v1.1):
- Windows 7 Taskbar
- Jump Lists, Icon Overlay, Progress Bar, Tabbed Thumbnails, and Thumbnail Toolbars
- Windows Shell
- Windows 7 Libraries
- Windows Shell Search API support
- Explorer Browser Control
- A hierarchy of Shell Namespace entities
- Windows Shell property system
- Drag and Drop for Shell Objects
- Windows Vista and Windows 7 Common File Dialogs, including custom controls
- Known Folders and non-file system containers
- Shell Object Watcher
- Shell Extensions API support
- Direct3D 11.0, Direct3D 10.1/10.0, DXGI 1.0/1.1, Direct2D 1.0, DirectWrite, Windows Imaging Component (WIC) APIs
- Windows Vista and Windows 7 Task Dialogs
- Sensor Platform APIs
- Extended Linguistic Services APIs
- Power Management APIs
- Application Restart and Recovery APIs
- Network List Manager APIs
- Command Link control and System defined Shell icons
The enhancements in this update of Code Pack are:
- Many FxCop violations and PREfast warnings have been addressed
- Bug fixes across various features and samples
- Includes many API improvements
- New features
- Shell Object Watcher
- Preview Handler APIs
- Thumbnail Handler APIs
- New samples for the new features
- Visual Studio 2010 compliance
- Initial xUnit test coverage
- String localization
- Signed assemblies
Here's a snap of the Samples (via WinDirStat)
Since we're focusing on properties, here's a snip of them from the doc's;
And specifically, Photo properties;
Yeah, wow. And all this is just the tip of the Code Pack archive.
BTW, I'm on Windows 8.1 and VS 2013 and the code samples seemed to run and work just fine...