Click here to Skip to main content
13,554,862 members
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

Generalapi amazon for search iTunes songs artworks Pin
lupin9829-May-08 13:44
memberlupin9829-May-08 13:44 

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
Web01-2016 | 2.8.180515.1 | Last Updated 21 Dec 2005
Article Copyright 2005 by yann bertaud
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid