Click here to Skip to main content
15,888,286 members
Articles / Programming Languages / XML

WMP Playlist Fix

Rate me:
Please Sign up or sign in to vote.
4.80/5 (2 votes)
6 Dec 2013CPOL6 min read 20.4K   546   5  
The goal is to clean-up double entries in Windows Media Player playlists because the player doesn’t do that itself. For that, a WPL class has been written, and a program that uses that class.
//
// WMPPlayListTool - Simple Windows Media Player Playlist Controller.
// Copyright (C) 2009-2013 John Janssen
// http://www.travelisfun.net 
//
// This program is free software; you can redistribute it and/or modify
// it any way you want as long as the above copyright statement is maintained.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
//

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Shell32;
using System.IO;

namespace WPLControlLib
{
  /// <summary>
  /// 
  /// </summary>
  public class MP3File
  {
    /// <summary>
    /// The filename without the path.
    /// </summary>
    public String FileName { get; private set; }
    /// <summary>
    /// The path of the file location.
    /// </summary>
    public string FilePath { get; private set; }

    /// <summary>
    /// The full path including filename.
    /// </summary>
    public String FullPath
    {
      get
      {
        if (FilePath.Length != 0)
          return Path.Combine(FilePath, FileName);
        return FileName;
      }
      set
      {
        FileName = value.Substring(value.LastIndexOf('\\') + 1);
        FilePath = value.Substring(0, value.LastIndexOf('\\'));
      }
    }
    public String FileExt
    {
      get
      {
        int pos = FileName.LastIndexOf('.');
        if (pos != -1)
          return FileName.Substring(pos);
        return String.Empty;
      }
    }
    public String ArtistName { get; set; }
    public String AlbumName { get; set; }
    public int TrackNumber { get; set; }
    public String SongTitle { get; set; }
    public MP3File()
    {
      FileName = string.Empty;
    }
    public MP3File(String filename)
    {
      Load(filename);
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="filename"></param>
    /// <returns></returns>
    public Boolean Load(String filename)
    {
      FullPath = filename;
      return Load();
    }
    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public Boolean Load()
    {
      if (!File.Exists(FullPath))
        return false;

      Boolean result = false;
      //create shell instance
      Shell32.Shell shell = new Shell();
      //set the namespace to file path
      Folder folder = shell.NameSpace(FilePath);
      //get ahandle to the file
      FolderItem folderItem = folder.ParseName(FileName);
      //did we get a handle ?
      if (folderItem != null)
      {
        //query information from shell regarding file
        int art = Environment.OSVersion.Version.Major < 6 ? 9 : 13;
        int alb = Environment.OSVersion.Version.Major < 6 ? 17 : 14;
        int song = Environment.OSVersion.Version.Major < 6 ? 10 : 21;
        int trk = Environment.OSVersion.Version.Major < 6 ? 19 : 26;
        ArtistName = folder.GetDetailsOf(folderItem, art);
        AlbumName = folder.GetDetailsOf(folderItem, alb);
        SongTitle = folder.GetDetailsOf(folderItem, song);
        String trackNumber = folder.GetDetailsOf(folderItem, trk);
        if (trackNumber.Length != 0)
        {
          int tracknum = 0;
          Int32.TryParse(folder.GetDetailsOf(folderItem, trk), out tracknum);
          TrackNumber = tracknum;
        }
        result = true;
      }
      //clean up
      folderItem = null;
      folder = null;
      shell = null;
      return result;
    }
  }
  /// <summary>
  /// 
  /// </summary>
  public class ShellID3TagControl
  {
    public static MP3File ReadID3Tags(String FileFullPath)
    {
      FileFullPath = Path.GetFullPath(FileFullPath);
      if (!File.Exists(FileFullPath))
        return null;
      try
      {
        //return mp3File instance
        return new MP3File(FileFullPath);
      }
      catch
      {
        return null;
      }
    }
  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
South Africa South Africa
Started in 1988 programming in Pascal, making a 68000 emulator on a DOS platform. Then from Pascal to Clipper, to C++ and now using C#. I've been programming for all walks of businesses; opticians, opthomologist, carbage collectors, reinforcement steel producers, retail chains, and more.
I'm now travelling through Africa with a home-build expedition truck.

Comments and Discussions