2

I'm trying to read all playlists and the music file in each playlist.

Here is my code in C#:

iTunesAppClass iTunesAppClass = new iTunesAppClass();
IITSourceCollection sources = iTunesAppClass.Sources;
foreach (IITSource src in sources)
{
    if (src.Name == "Library")
    {
        IITPlaylistCollection pls = src.Playlists;
        foreach (IITPlaylist p in pls)
            if (p.Kind == ITPlaylistKind.ITPlaylistKindUser)
            {
                var pname = p.Name;
                IITTrackCollection tracks = p.Tracks;
                foreach (IITTrack track in tracks)
                {
                    var name = track.Name;
                    string filename = "???";   // How to get the file name of the mp3 file?
                }
            }
    }
}

So, in the last line, I get Track.Name which seems to be the Title of the song.

How can I get the full path and file name of the track?

Raptor
  • 53,206
  • 45
  • 230
  • 366
FLICKER
  • 6,439
  • 4
  • 45
  • 75

1 Answers1

0

You worked on such an ancient library. Here is the way to find the file name:

foreach (IITTrack track in tracks)
{
    var name = track.Name;
    if (track.Kind == ITTrackKind.ITTrackKindFile) { // it is a file track
        // Cast it as file track
        IITFileOrCDTrack fileTrack = (IITFileOrCDTrack)track;
        // Check if it's a valid path
        if (!String.IsNullOrEmpty(fileTrack.Location)) {
            string filepath = fileTrack.Location; // here you go
        }
    }
}

I didn't actually run the code above (I found similar code in a snippet written in 2007). You can try it by yourself.

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
Raptor
  • 53,206
  • 45
  • 230
  • 366