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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • The Tectonic AI Platform: A Framework for Taming App Sprawl and Data Fragmentation
  • Metadata, Not Data Volume, Is the Real Bottleneck in Modern Data Lakes
  • From Data Lakes to Intelligence Lakes: Augmenting Apache Iceberg With Generative AI Metadata on AWS
  • How to Improve Copilot's Accuracy and Performance in Power BI

Trending

  • How an AI Software Factory Works: From Developer Intent to Production
  • GenAI Security Gateway: An End-to-End Architecture
  • One Database, Two Models: Building AI-Friendly Java Applications With MySQL JSON Duality Views
  • Why Spark Executors Leave Cores Idle on YARN: A Packing Model for Executor Sizing

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. Databases
  4. Extracting File Metadata with C# and the .NET Framework

Extracting File Metadata with C# and the .NET Framework

How to extract extended image metadata using C# and the Windows API Code Pack, simplifying access to detailed file properties typically seen in Windows Explorer.

By 
Rob Sanders
·
Oct. 26, 13 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
40.2K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • The Tectonic AI Platform: A Framework for Taming App Sprawl and Data Fragmentation
  • Metadata, Not Data Volume, Is the Real Bottleneck in Modern Data Lakes
  • From Data Lakes to Intelligence Lakes: Augmenting Apache Iceberg With Generative AI Metadata on AWS
  • How to Improve Copilot's Accuracy and Performance in Power BI

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook