« The Humor of ReSharper 7 | Main | An RGB Atlas of Colors - Just a page after page of sweet, glorious RGB »

Exporting Windows Media Player Metadata to a CSV file  

I've written about this topic before except at that time dealing with IronPython and PowerShell. This post shows you show to accomplish the same thing in C#.

In order to use this code you'll need to:


  • Add a reference to WMPLib which is found at C:\Windows\System32\wmp.dll.
  • Add a NuGet reference to CsvHelper



using System.Collections.Generic;
using System.Linq;

// Add a Reference to WMPLib at c:\windows\system32\wmp.dll
// for VS2010 and VS2010 - make sure that for the WMPLib 
//    reference you set Embed Interop Types to FALSE
using System.IO;
using CsvHelper;

namespace ExportWMPMetdataToCSV
    class Program
        static void Main(string[] args)
            if (args.Length < 1)
                System.Console.WriteLine("Syntax: ExportWMPMetadataToCSV.exe  []");

            int? top = null;

            if (args.Length >= 2)
                top = int.Parse(args[1]);

            string output_filename = args[0];
            var normalized_names = new Dictionary();
            foreach (var attr in all_attributes)
                string name = attr.Replace("/", "_");
                normalized_names[attr] = name;

            var wmp = new WMPLib.WindowsMediaPlayerClass();
            var item_collection = wmp.mediaCollection.getAll();

            using (var writer = File.CreateText(output_filename))
            using (var csv = new CsvWriter(writer))
                // Write CSV Header
                for (int i = 0; i < all_attributes.Length; i++)
                    string h = normalized_names[all_attributes[i]];

                for (int i = 0; i < item_collection.count; i++)
                    if (top.HasValue && i>= top.Value)

                    // write a csv line for each item in the collection
                    var item = item_collection.Item[i];

                    for (int j = 0; j < all_attributes.Length; j++)
                        var attr_value = item.getItemInfo(all_attributes[j]) ?? "";

                    string sourceurl = item.getItemInfo("SourceURl") ?? "";
                    if (sourceurl.Length > 0)
                        if (sourceurl[1] == ':')
                            if (!System.IO.File.Exists(sourceurl))
                                System.Console.WriteLine("does not exist: {0} {1}", i+1,sourceurl);




        private static string[] all_attributes = new string[]

PrintView Printer Friendly Version

EmailEmail Article to Friend

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.
Author Email (optional):
Author URL (optional):
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>