Click here to Skip to main content
12,881,367 members (30,087 online)
Click here to Skip to main content
Add your own
alternative version


37 bookmarked
Posted 20 Dec 2005

Accessing songs and playlists from ITunes using C#

, 20 Dec 2005
Rate this:
Please Sign up or sign in to vote.
How to get the song list, song information from ITunes using .NET.


This is a simple example showing how to interact with ITunes version 6. The sample shows how to connect to Itunes version 6 using C# and how to get the list of songs in the itunes library or in the saved playlists.

The project was built in VS 2005. A reference to the COM object ITunes 1.6 needs to be added to your project. You can get the itunes SDK from here. The help file included in the zip was fairly useful.

Digging around on the internet, I found this site which helped me get started on this little project.

Using the code

I think the comments within the code are self explanatory:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using iTunesLib;
namespace WindowsApplication1
    public partial class Form1 : Form
        public Form1()
        // initialize Itunes application connection
        iTunesApp itunes = new iTunesLib.iTunesApp();
        private void Form1_Load(object sender, EventArgs e)
             // get main library playlist
             IITLibraryPlaylist mainLibrary = itunes.LibraryPlaylist;
             // get list of all playlists defined in 
             // Itunes and add them to the combobox
             foreach (IITPlaylist pl in itunes.LibrarySource.Playlists)
             // get the tracks within the mainlibrary
             // set the datagridview1 datasource to the datatable.
             dataGridView1.DataSource = dataTable1;

        // getthe tracks from the the specified playlist
        private void GetTracks(IITPlaylist playlist)
             long totalfilesize = 0;
             // get the collection of tracks from the playlist
             IITTrackCollection tracks = playlist.Tracks;
             int numTracks = tracks.Count;
             for (int currTrackIndex = 1; 
                 currTrackIndex <= numTracks; currTrackIndex++)             
                  DataRow drnew = dataTable1.NewRow(); 
                  // get the track from the current tracklist                 
                  IITTrack currTrack = tracks[currTrackIndex];
                  drnew["artist"] = currTrack.Artist;
                  drnew["song name"] = currTrack.Name;
                  drnew["album"] = currTrack.Album;
                  drnew["genre"] = currTrack.Genre;
                  // if track is a fiile, then get the file 
                  // location on the drive. 
                  if (currTrack.Kind == ITTrackKind.ITTrackKindFile)
                      IITFileOrCDTrack file = (IITFileOrCDTrack)currTrack;
                      if (file.Location != null)
                           FileInfo fi = new FileInfo(file.Location);
                               if (fi.Exists)
                                    drnew["FileLocation"] = file.Location;
                                    totalfilesize += fi.Length;
                                    drnew["FileLocation"] = 
                                             "not found " + file.Location;
              lbl_numsongs.Text = 
                  "number of songs: " + dataTable1.Rows.Count.ToString() + 
                  ", total file size: " + 
                  (totalfilesize / 1024.00 / 1024.00).ToString("#,### mb");
          private void comboBox1_SelectedIndexChanged(object sender, 
                                                              EventArgs e)
              // get list of tracks from selected playlist
              string playlist = comboBox1.SelectedItem.ToString();
              foreach (IITPlaylist pl in itunes.LibrarySource.Playlists)
                   if (pl.Name == playlist)

Hope you find this useful, any comments/feedback appreciated.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

yann bertaud
Web Developer
United States United States
Employed for the last 15 years at Autodesk, in San Francisco, CA.

Currently senior Software Quality Engineer in Autodesk Media Entertainment Division working on 3ds max.

You may also be interested in...


Comments and Discussions

QuestioniTunes over Network Pin
Humschi12-Jun-12 21:51
memberHumschi12-Jun-12 21:51 
QuestionTrouble adding tracks to a play Pin
Kombu19-Aug-11 9:24
memberKombu19-Aug-11 9:24 
GeneralCom exception Pin
Member 443375531-Aug-08 23:33
memberMember 443375531-Aug-08 23:33 
Generalapi amazon for search iTunes songs artworks Pin
lupin9829-May-08 13:44
memberlupin9829-May-08 13:44 
QuestionWindows2000 Pin
Sunshine Always26-Feb-08 22:32
memberSunshine Always26-Feb-08 22:32 
QuestionHow to get Smart Info and Smart Criteria Pin
guangstat13-Aug-07 20:03
memberguangstat13-Aug-07 20:03 
QuestionHow to get only tracks out of "MUSIC" Pin
ahaudi18-Jul-07 7:09
memberahaudi18-Jul-07 7:09 
QuestionHow to get the music folder location of iTunes by iTunes SDK? Pin
pudding3327-Apr-07 16:44
memberpudding3327-Apr-07 16:44 
Generaladd playlists in itumes Pin
trxrst23-Apr-07 4:44
membertrxrst23-Apr-07 4:44 
GeneralSweet, possible to get the filepath of track Pin
salle7824-Mar-07 3:01
membersalle7824-Mar-07 3:01 
Generalstreaming track from iPod to PC Pin
Halid Niyaz22-Aug-06 20:23
memberHalid Niyaz22-Aug-06 20:23 
Questionhow do you get at the artwork? Pin
hillscottc1-Jan-06 21:11
memberhillscottc1-Jan-06 21:11 
AnswerRe: how do you get at the artwork? Pin
tayspen5-Jan-06 14:32
membertayspen5-Jan-06 14:32 
GeneralRe: how do you get at the artwork? Pin
scott.hill6-Jan-06 11:50
memberscott.hill6-Jan-06 11:50 
GeneralRe: how do you get at the artwork? Pin
tayspen7-Jan-06 6:16
membertayspen7-Jan-06 6:16 
GeneralRe: how do you get at the artwork? Pin
tayspen20-Jan-06 11:19
membertayspen20-Jan-06 11:19 
GeneralRe: how do you get at the artwork? Pin
yann bertaud20-Jan-06 16:14
memberyann bertaud20-Jan-06 16:14 
GeneralRe: how do you get at the artwork? Pin
tayspen21-Jan-06 8:48
membertayspen21-Jan-06 8:48 
GeneralRe: how do you get at the artwork? Pin
Drew Noakes21-Jan-06 12:30
memberDrew Noakes21-Jan-06 12:30 
GeneralRe: how do you get at the artwork? Pin
tayspen27-Jan-06 0:49
membertayspen27-Jan-06 0:49 
GeneralRe: how do you get at the artwork? Pin
tayspen6-Feb-06 17:09
membertayspen6-Feb-06 17:09 
GeneralNice...but.... Pin
tayspen24-Dec-05 11:10
membertayspen24-Dec-05 11:10 
GeneralRe: Nice...but.... Pin
robroe27-Dec-05 23:00
memberrobroe27-Dec-05 23:00 
I think it is possible to get the playlists from the iPod, but what is your reason for doing this as the playlists on iTunes are normally the same as those on the iPod unless you manually sync them?

There is good example code in the help file which is mentioned in this article but you could try something like this (the ipod needs to be mounted) :
<br />
iTunesApp itunes = new iTunesLib.iTunesApp();<br />
<br />
IITSourceCollection sources = itunes.Sources;<br />
<br />
//Find the iPod source (where "iPod" is the name of your iPod)<br />
IITSource iPod = sources.get_ItemByName("iPod");<br />
<br />
foreach (IITPlaylist pl in iPod.Playlists)<br />
{<br />
   //do something<br />
}<br />

This is the bare bones and needs error handling etc... The iPod is also a special type of source (IITIPodSource) which derives from IITSource so you may want to cast it to this so that you can get the additional functionality.

-- modified at 5:00 Wednesday 28th December, 2005
GeneralRe: Nice...but.... Pin
tayspen28-Dec-05 5:45
membertayspen28-Dec-05 5:45 
GeneralRe: Nice...but.... Pin
Neil Lamka28-Dec-05 7:57
memberNeil Lamka28-Dec-05 7:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170422.1 | Last Updated 21 Dec 2005
Article Copyright 2005 by yann bertaud
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid