Click here to Skip to main content
11,577,143 members (63,596 online)
Click here to Skip to main content

ID3 Tag Reader Using Shell Functions

, 21 Sep 2003 Public Domain 178K 5K 69
Rate this:
Please Sign up or sign in to vote.
Utility to read ID3 tags from MP3 files using Windows Shell functions.

Introduction

This article describes another alternative for reading and displaying ID3 tags in MP3 files. There are various well documented methods of extracting ID3 tag information from MP3 files available. One of the most useful sites regarding this information is http://www.id3.org. This article will explore the possibility of extracting the same information using Win32 Shell functions.

Background

The Shell32.dll is responsible for a host of functions from launching applications to creating shortcuts and managing printers. You can read more about all the wonderful functionality it offers here. With Windows XP, users get the option of selecting a wide variety of columns to display when Explorer is in "Detail" view. Most of the extra columns are centered around multimedia files such as MP3 tracks and digital pictures. By way of the functionality provided in Shell32.dll, it is possible to display ID3 tag information for MP3 audio files and embedded EXIF information in JPEG picture files.

Using the code

The basic functionality of the ID3 tag reader is implemented as a struct with a single static ReadID3Tags method that returns a user defined MP3File object. It is beneficial to implement legacy COM wrapper functionality in a struct, as struct types get allocated in the heap and hence are exempt from garbage collection. They are also lightweight (compared to object instances), as they relieve the CLR from the task of reference counting.

We begin by adding a reference to Shell32.dll.

The MP3File class is basically a skeleton placeholder for the ID3 information returned from our Shell32 wrapper.

using System;
namespace ShellID3Reader {
    public class MP3File {
        private string fileName;
        private string artistName;
        private string albumName;
        private int trackNumber;
        private string songTitle; 

        public string FileName {
             get{return this.fileName;}
            set{this.fileName = value;}
        }
        public string ArtistName {
            get{return this.artistName;}
            set{this.artistName = value;}
        }
        public string AlbumName {
            get{return this.albumName;}
            set{this.albumName = value;}
        }
        public int TrackNumber {
             get{return this.trackNumber;}
            set{this.trackNumber = value;}
        }
        public string SongTitle {
             get{return this.songTitle;}
            set{this.songTitle = value;}
        }
        public MP3File(){
        }
    }
}

The ShellID3TagReader struct is responsible for reading the ID3 tags from MP3 files. Its takes the full file path and name as an input parameter and returns an instance of MP3File after decorating its properties, with information extracted from the Windows shell.

using System;
namespace ShellID3Reader
{
    public struct ShellID3TagReader
    {
        public static MP3File ReadID3Tags(string FileFullPath){
            MP3File mp3File = new MP3File();

            //parse file name
            string fileName = 
              FileFullPath.Substring(FileFullPath.LastIndexOf("\\")+1);
            //parse file path
            string filePath = 
              FileFullPath.Substring(0,FileFullPath.LastIndexOf("\\"));
            //create shell instance
            Shell32.Shell shell  = new Shell32.ShellClass();
            //set the namespace to file path
            Shell32.Folder folder = shell.NameSpace(filePath);
            //get ahandle to the file
            Shell32.FolderItem folderItem = folder.ParseName(fileName);
            //did we get a handle ?
            if (folderItem !=null){
                mp3File.FileName = fileName;
                //query information from shell regarding file
                mp3File.ArtistName = folder.GetDetailsOf(folderItem,9);
                mp3File.AlbumName = folder.GetDetailsOf(folderItem,17);
                mp3File.SongTitle = folder.GetDetailsOf(folderItem,10);
                mp3File.TrackNumber = 
                  Int32.Parse(folder.GetDetailsOf(folderItem,19));
            }
            //clean ip
            folderItem = null;
            folder = null;
            shell = null;
            //return mp3File instance
            return mp3File;
        }
    }
}

And finally we add the MP3File instance to a ListView for display.

private void cmdReadTags_Click(object sender, System.EventArgs e) {
            
    listViewFiles.Columns.Clear();
    listViewFiles.Items.Clear();
    listViewFiles.Columns.Add("FileName",100,HorizontalAlignment.Left); 
    listViewFiles.Columns.Add("ArtistName",100,HorizontalAlignment.Left); 
    listViewFiles.Columns.Add("AlbumName",100,HorizontalAlignment.Left); 
    listViewFiles.Columns.Add("TrackNumber",100,HorizontalAlignment.Left); 
    listViewFiles.Columns.Add("SongTitle",100,HorizontalAlignment.Left); 
            
    DirectoryInfo dir = new DirectoryInfo(textBoxFilePath.Text);
    FileInfo[] files = dir.GetFiles("*.mp3");
    foreach(FileInfo fi in files){
        MP3File mp3File = ShellID3TagReader.ReadID3Tags(fi.FullName);
        ListViewItem itm = new ListViewItem();
        itm.Text = mp3File.FileName;
        itm.SubItems.Add(mp3File.ArtistName);
        itm.SubItems.Add(mp3File.AlbumName );
        itm.SubItems.Add(mp3File.TrackNumber.ToString()) ;
        itm.SubItems.Add(mp3File.SongTitle);
        listViewFiles.Items.Add(itm);
    }
}

History

  • Sept 23 2003 - Initial version (1.0.0)

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication

Share

About the Author

Erhan Hosca
Web Developer JPMorgan Chase & Co.
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 3 Pin
snsrkrishna16-Sep-11 7:13
membersnsrkrishna16-Sep-11 7:13 
the demo is not working, but the article is good and useful
Generala simple tutorial Pin
sandeepparekh30-May-11 16:51
membersandeepparekh30-May-11 16:51 
GeneralProblem with multiple artists Pin
Soundman32.228-Aug-08 3:59
memberSoundman32.228-Aug-08 3:59 
GeneralCode that works with Vista and XP, using Shell32 Pin
Javawag20-Aug-08 6:32
memberJavawag20-Aug-08 6:32 
QuestionIs there any way to retrieve the year? Pin
tequilalove28-Apr-08 0:28
membertequilalove28-Apr-08 0:28 
AnswerRe: Is there any way to retrieve the year? Pin
Erhan Hosca11-May-08 15:56
memberErhan Hosca11-May-08 15:56 
GeneralIDs seem to have changed in Vista Pin
DylanV21-Dec-07 13:41
memberDylanV21-Dec-07 13:41 
GeneralVista item indexes. Pin
DylanV21-Dec-07 13:40
memberDylanV21-Dec-07 13:40 
GeneralRe: Vista item indexes. Pin
StephenQF25-Jan-09 19:57
memberStephenQF25-Jan-09 19:57 
GeneralA few tips Pin
reinux26-Oct-06 22:16
memberreinux26-Oct-06 22:16 
QuestionWriting ID3 Tag information Using Shell32 Pin
getprashanthr15-Dec-05 20:22
membergetprashanthr15-Dec-05 20:22 
GeneralReading ID3 Tag from binary files Using Shell32 Pin
inams1-Dec-05 18:34
memberinams1-Dec-05 18:34 
GeneralRe: Reading ID3 Tag from binary files Using Shell32 Pin
Erhan Hosca2-Dec-05 15:58
memberErhan Hosca2-Dec-05 15:58 
QuestionHow to read .m4a (aac) Pin
tayspen21-Oct-05 9:28
membertayspen21-Oct-05 9:28 
AnswerRe: How to read .m4a (aac) Pin
jesseseger2-Jan-10 17:45
memberjesseseger2-Jan-10 17:45 
Generalread/write audio metadata Pin
laurence fass20-Sep-05 5:18
memberlaurence fass20-Sep-05 5:18 
QuestionRe: read/write audio metadata Pin
getprashanthr15-Dec-05 20:20
membergetprashanthr15-Dec-05 20:20 
GeneralNot Reading Mp3 Tags Pin
empeyg15-Jun-05 11:46
memberempeyg15-Jun-05 11:46 
GeneralRe: Not Reading Mp3 Tags Pin
Erhan Hosca15-Jun-05 15:13
memberErhan Hosca15-Jun-05 15:13 
GeneralRe: Not Reading Mp3 Tags Pin
Stephen Remde (smremde)11-Aug-06 10:34
memberStephen Remde (smremde)11-Aug-06 10:34 
GeneralDoesn't seem to work on Windows 2000 Pin
Ashley van Gerven19-Mar-05 18:37
memberAshley van Gerven19-Mar-05 18:37 
GeneralRe: Doesn't seem to work on Windows 2000 Pin
AR12320-Apr-05 20:00
memberAR12320-Apr-05 20:00 
GeneralRe: Doesn't seem to work on Windows 2000 Pin
anonymous28-May-05 4:09
sussanonymous28-May-05 4:09 
GeneralOther column indexes.. (NT5.1-5.2 ONLY) Pin
Niels Penneman19-Feb-04 10:36
memberNiels Penneman19-Feb-04 10:36 
GeneralRe: Other column indexes.. (NT5.1-5.2 ONLY) Pin
tayspen25-Nov-05 17:17
membertayspen25-Nov-05 17:17 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150603.1 | Last Updated 22 Sep 2003
Article Copyright 2003 by Erhan Hosca
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid