DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Data
  4. Extracting File Metadata with C# and the .NET Framework

Extracting File Metadata with C# and the .NET Framework

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?

Rob Sanders user avatar by
Rob Sanders
·
Oct. 14, 13 · Tutorial
Like (0)
Save
Tweet
Share
59.73K Views

Join the DZone community and get the full member experience.

Join For Free

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

imageWhen 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

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:

image
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. 

image

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:

image

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.

Framework Property (programming) Data (computing) Metadata

Published at DZone with permission of Rob Sanders, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How To Create and Edit Excel XLSX Documents in Java
  • Core Machine Learning Metrics
  • Do Not Forget About Testing!
  • Promises, Thenables, and Lazy-Evaluation: What, Why, How

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: