Note to self: Reading extended file properties

Wednesday, January 05, 2011 / Posted by Luke Puplett /

To read the extended file property metadata from a file, such as a the exposure of a photo or the duration of some media file, then follow these simple steps.

Download and reference the Windows API Code Pack, no link here since it changes name and version, but it easily found in Bing.

// Usings

using System;
using System.IO;
using Microsoft.WindowsAPICodePack.Shell;
using Microsoft.WindowsAPICodePack.Shell.PropertySystem;

// Method

string mediaPath = @"c:\Users\Public\Recorded TV\Shark Tale_BBC ONE_2010_12_31_14_08_00.wtv";

var file
= new FileInfo(mediaPath);

// The formatId and propertyId values can be taken from MSDN:
PropertyKey pk = new PropertyKey("64440490-4C8B-11D1-8B70-080036B11A03", 3);
var p
= ShellFile.FromFilePath(file.FullName).Properties.GetProperty(pk);

if (p != null)
string.Concat(p.CanonicalName, ": ", p.ValueAsObject));

UInt64 d
= (UInt64)p.ValueAsObject;
var duration
= TimeSpan.FromTicks((long)d);

Labels: ,


Post a Comment