Click here to Skip to main content
13,141,157 members (62,196 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 
I have got it, it now does what i want, which is add the album art when nay song is played in iTunes. This is how i did it. First you add the playerchangeevent, this is raised when the song is changed in itunes.

tunes.OnPlayerPlayEvent += new _IiTunesEvents_OnPlayerPlayEventEventHandler(iTunes_OnPlayerPlayEvent);//Declare Event

Hereis what we do on the Event

<br />
 private void iTunes_OnPlayerPlayEvent(object o)<br />
        {<br />
            string Album = tunes.CurrentTrack.Album.ToString(); //This is the album of current songthats playing<br />
            string Artist = tunes.CurrentTrack.Artist.ToString(); //Artist of current playing song<br />
            Thread.Sleep(2000);<br />
            if (tunes.CurrentTrack.Artwork.Count < 1)<br />
            {<br />
                GetArt(Artist, Album);<br />
                iTuensArtistlbl.Text = artist;//This is just UI stuff<br />
                iTunesAlbumlbl.Text = name; //this to<br />
            }<br />
            else<br />
            {<br />
                try<br />
                {<br />
          [1].SaveArtworkToFile(string.Concat(Application.StartupPath, "\\albumArt.jpg"));<br />
                    FileStream fileStream = new FileStream(string.Concat(Application.StartupPath, "\\albumArt.jpg"), FileMode.Open, FileAccess.Read);<br />
                    this.iTunesArtPic.SizeMode = PictureBoxSizeMode.StretchImage;<br />
                    this.iTunesArtPic.Image = Image.FromStream(fileStream);<br />
                    fileStream.Close();<br />
                    Application.DoEvents();<br />
//Thatocde attempts to display the art if the song already had it.<br />
                }<br />
                catch (Exception e)<br />
                {<br />
                    MessageBox.Show(e.Message);<br />
                }<br />
            }<br />
        }<br />

And here is the get art method, you will need an amazon dev tag to use this.

<br />
 public void GetArt(string artistname, string albumtitle)<br />
        {<br />
            string request = artistname + " " + albumtitle;<br />
<br />
            KeywordRequest req = new KeywordRequest();<br />
            req.keyword = request;<br />
            req.devtag = MyTag;<br />
            req.mode = "music";<br />
            req.type = "lite";<br />
   = "1";<br />
<br />
            AmazonSearchService search = new AmazonSearchService();<br />
<br />
            try<br />
            {<br />
                ProductInfo productInfo = search.KeywordSearchRequest(req);<br />
<br />
                if (productInfo.Details.Length != 0)<br />
                {<br />
                    string result;<br />
                    result = productInfo.Details[0].ImageUrlLarge;<br />
                    System.Net.WebClient c = new System.Net.WebClient();<br />
                    System.Threading.Thread.Sleep(2000);<br />
                    c.DownloadFile(result, "C:\\art.jpg");<br />
                    tunes.CurrentTrack.AddArtworkFromFile("C:\\art.jpg");<br />
                    Application.DoEvents();<br />
                    File.Delete("C:\\art.jpg");<br />
                }<br />
            }<br />
            catch<br />
            {<br />
                // Amazon seems to throw this exception even if the only problem<br />
                // is that keyword request returned no result, so simply do nothing..<br />
            }<br />
<br />
            Application.DoEvents();<br />
        }<br />

Pretty straight forward code...

I would like to improve this bypresenting a use with the list of albums and they check which ones they want and it gets all art for the whole album. Any Ideas?

This code was obvioulsy the basis, of the album art idea.


-- modified at 23:10 Monday 6th February, 2006
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 
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
Web03 | 2.8.170915.1 | Last Updated 21 Dec 2005
Article Copyright 2005 by yann bertaud
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid