Search code examples
c#wpfmetadatajpegxmp

Trying to write to the Keywords windows metadata item using C#


We have a couple folks, right now, going through a directory of thousands of jpgs and hand-setting the Tags metadata item on them in Windows explorer. I want to whip something up to just read the spreadsheet they're getting the tags from and applying them to the files.

In the research I've done, it seems that the "Tags" metadata in Windows is actually the "Keywords" xmp metadata. I've successfully managed to read this metadata using the following code:

FileStream fs = new FileStream(@"C:\test.jpg", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
        BitmapSource img = BitmapFrame.Create(fs);
        BitmapMetadata md = (BitmapMetadata)img.Metadata;
        StringBuilder tags = new StringBuilder();
        foreach (String s in md.Keywords)
        {
            tags.Append(s);
            tags.Append(";");
        }
        String fin = tags.ToString();

The problem is that the Keywords property is a read-only collection. I've found several "leads" on writing to this metadata, but none of them seem to work. It especially sucks because if I open the file in notepad++, I can see exactly where the metadata is stored, but the number of xml tags around them is messy enough that trying to just manually jam things in there won't work. Does anyone have any information on how to write to the Keywords jpg metadata in Windows using C#?

Just to show I really tried, here's a bunch of links I've tried using:


Solution

  • After a lot more googling (with keywords that don't make a lot of sense), I finally found this other question which tells how to do it with the Windows API code pack: Is it possible to set/edit a file extended properties with Windows API Code Pack?

    To get the codepack, open the pack manager console in VS and enter Install-Package WindowsAPICodePack

    MS themselves have pulled this package from MSDN's archive, so NuGet is the only way to get it. Figures they'd make this 50x harder than it needed to be, especially since the MSDN article about their property system brags about how it's the future of managing files in Windows.