Monday, October 28, 2013

Toward Metadata Mastery with the Windows API Code Pack

DZone - Rob Sanders - Extracting File Metadata with C# and the .NET Framework

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.

...

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:

...

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.

...

The SystemProperties class defines an incredibly useful hierarchy of extended property identifiers which can be used in conjunction with shell objects, as demonstrated above

image

What is this Windows API Code Pack thing?

Windows® API Code Pack for Microsoft® .NET Framework

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:

  • 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
  • DirectX
    • 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
What’s New in this update (v1.1):
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

Sure I've know about the Windows API Code Pack for years, but it never clicked that I could use it to get this kind of file metadata... sigh... so much to learn, so few brain cells (left). Yes, it's dated, but the source is available...

 

Related Past Post XRef:
How many ways are there to create a File Watcher? Well, here's three at least...
Windows API Code Pack v1.1 Released (Think “Bug fix, new Shell/Thumbnail/Preview features, VS2010, xUnit, string localization, signed assemblies and more” release)
More Windows Code Pack and Windows 7 developing delightfulness
Windows 7 Code Pack v1.0 Released – Managed code to help get at the yummy Windows 7 filling…
Bridging to Managed Windows 7 Development – Vista Bridge to Windows Code Pack
Vista Bridge v1.4 Released – Now with custom controls for Common File Dialogs, BreadCrumb control and Aero Glass
Vista Bridge (v1.3) available on MSDN Code Gallery
It’s the Bridge to… Vista – Getting and installing Vista Bridge Sample Library

No comments: