共用方式為


Automating iTunes with C# in .NET

When you install Apple iTunes, it includes a COM automation library (see link for SDK docs).  It is very easy to use!  Thanks to Dan Crevier’s blog post for pointing me in the right direction to the SDK.

I needed to remove all the tracks in iTunes that no longer exist on my disk (I move my files around a lot).  The SDK included a JScript example that I converted to C# .NET (below).

Just create a new C# Console application, copy/paste this into Program.cs, and change the project properties from a Console application to a Windows Application, and look for the app’s output in the Output Window.  You’ll also need to add a project reference to the COM library “iTunes 1.7 Type Library”.

Here is the sample code: iTunesRemoveDeadFiles.zip

using System;

using iTunesLib;

namespace RemoveDeadFiles

{

   class Program

   {

      static void Main(string[] args)

      {

         // iTunes classes

         iTunesAppClass itunes = new iTunesAppClass();

         IITLibraryPlaylist mainLibrary = itunes.LibraryPlaylist;

         IITTrackCollection tracks = mainLibrary.Tracks;

         IITFileOrCDTrack currTrack;

         // working variables

         int numTracks = tracks.Count;

         int deletedTracks = 0;

         while (numTracks != 0)

         {

            // only work with files

            currTrack = tracks[numTracks] as IITFileOrCDTrack;

            // is this a file track?

            if (currTrack != null && currTrack.Kind == ITTrackKind.ITTrackKindFile)

            {

               // yes, does it have an empty location?

               if (currTrack.Location == null)

               {

                  // yes, delete it

                  currTrack.Delete();

                  deletedTracks++;

               }

            }

            // progress to the next tack

            numTracks--;

         }

         // report to the user the results

         System.Diagnostics.Trace.WriteLine(

            String.Format("Removed {0} track{1}.", deletedTracks,

            deletedTracks == 1 ? "" : "s"));

      }

   }

}

Comments

  • Anonymous
    July 06, 2006
    I've been doing this for a while. I have a program that will switch back and forth between Apple Lossless and MP3 encoding rather than having to go through then menu's each time.

  • Anonymous
    July 28, 2006
    Hi!
    I have a problem
    I use Microsoft Visual C# 2005, I added a reference to the COM library and I tried to create an instance of the "iTunesAppClass". When i try to acces a member of the instance, it tells me: "A field initializer cannot reference the nonstatic field, method, or property".
    I copied the code exactly as it is

    Do you know what the problem is?
    If you do, please answer me to "naio13@gmail.com"
    Thanks!!!

    regards ....

  • Anonymous
    February 20, 2007
    Hey naio, Sorry, I don't know what's going on.  I just copied the code into a .cs file, added the iTunesLib reference, and it ran fine.  Maybe they're a problem with the iTunes installation??  If you find out, please reply with what's up.

  • Anonymous
    February 21, 2007
    Hey i am trying to convert audio files present in my local disk to mp3 format using c#.  I successfully imported my audio files to itunes library, but now i want to itunes to convert it using mp3 encoder (instead of default aac). Can any one help me out how   to code this in c#.... Thanks in advance

  • Anonymous
    July 07, 2007
    You've been kicked (a good thing) - Trackback from DotNetKicks.com

  • Anonymous
    March 17, 2008
    Really good information fro windows based application; but how can I display the library [list of albums/songs] in a web based application and how the SDK helps here? Thanks in advance.

  • Anonymous
    May 05, 2008
    Gopi: Unless you control the hosting environment it may be infeasible - but if you host yourself, it should be straightforward. iTunes and the COM library will have to be installed on the web server. Use IiTunes.LibraryPlayList to get the collection of all tracks in the library - just like in the code sample on this very page. Basically, the code concerning iTunes stuff, such as getting a list of tracks, for a web app will be no different from any other application, but from a hosting perspective it does mean your app will need sufficient trust to do COM interop and the server must have iTunes installed as well.

  • Anonymous
    June 04, 2008
    hai ,    your application is  some what helpful for me ,but can u give suggetion on how to convert any audeo codec to mp3 .I willl be thankfull to you if you give me suggetion (email at:pradipanantha@gmail.com) thank you,

  • Anonymous
    September 08, 2008
    Thanks.  I've been looking for something like this.

  • Anonymous
    December 12, 2008
    does any one knows, how to get the how long is a Song? I mean the duration (time) of the current song, I have been search for a while, but I can't find any attribute of the IITTrack object who hold this one, please if some know the answer I will be very very greatful. thanks from MExico.. :)

  • Anonymous
    December 12, 2008
    sorry I found it already :P ..

  • Anonymous
    February 22, 2009
    is it possible to export the itunes library to a text file using c# (ie call the File > Export function)? if the only way to do it is to loop through each song like the above example and write the details to a text file then it will take too long to be worth while. thanks in advance

  • Anonymous
    March 25, 2011
    is it possible to check the throughput of data tranfers to ipod or iphone using COM lib

  • Anonymous
    April 18, 2013
    Hi! How could I create a playlist inside a playlist folder? I can create a folder and a playlist as you can see in the next code snippet, but I can't figure out how to create a playlist inside a folder. iTunesApp itunes = new iTunesLib.iTunesApp(); IITLibraryPlaylist mainLibrary = itunes.LibraryPlaylist; IITTrackCollection tracks = mainLibrary.Tracks; IITPlaylist folder = itunes.CreateFolder("Folder"); IITPlaylist pl= itunes.CreatePlaylist("Playlist"); Thanks!

  • Anonymous
    April 29, 2013
    Hi, I have an application that records audio to a .wav file. It's written in VB, but I'm looking for something in particular about the iTunesApp. After the file(s) has/have been recorded I convert it/them to .wma using Windows Media Encoder (through VB). The converted files go to a specific folder (based on certain user input), and are not in the "My Music" folder or sub-folders (that's the way that I want it). Recently, I was asked if there was a way to create iTunes versions of the files so that I could email them to students with iOS devices (currently, I burn the .wma files to a CD and produce a directory of the CD). I did a little research and came upon the following code/commands that I have inserted into my program: Imports iTunesLib Dim obj_iTunesApp As iTunesApp obj_iTunesApp.ConvertFile2(strWavFileName) The ConvertFile2 works pretty well. I end up with an .m4a file. The problem is: (1) How do I tell it the destination file (like Windows Media Encoder does), (2) How do I give it a Title, Artist, etc (like Windows Media Encoder does). Thanks for your time in advance

  • Anonymous
    October 12, 2013
    I found that you have to build the solution for x86 otherwise you get som COM errors. So make sure that the Target CPU in the project is set to x86.

  • Anonymous
    November 12, 2013
    hi. I want to burn  many audible playlist automatic but i can't found lib suport  for my problem. please hepl me thanks!