/// <summary> /// Creates a new music file /// </summary> /// <param name="file"></param> public MusicFile(string file) { // Store the file File = file; // The title is the file name Title = Path.GetFileNameWithoutExtension(file); try { // Mp3 file if (Path.GetExtension(file).ToLowerInvariant() == ".mp3") { // Open the file using (FileStream fileStream = new FileStream(File, FileMode.Open)) { // Use the mp3 reader using (var mp3Stream = new Mp3(fileStream, Mp3Permissions.Read)) { // Has tags if (mp3Stream.HasTags) { // Search in all tags foreach (var tag in mp3Stream.GetAllTags()) { // Title if (tag.Title.IsAssigned) { Title = RemoveInvalidCharsAndTrim(tag.Title); } // Album if (tag.Album.IsAssigned) { Album = RemoveInvalidCharsAndTrim(tag.Album); } // Artist if (tag.Artists.IsAssigned) { Artists = RemoveInvalidCharsAndTrim(tag.Artists); } // We take the first tag with name we find if (tag.Title.IsAssigned) { break; } } } } } } } catch (Exception ex) { Console.WriteLine("Could not read id3 tag from file: " + file); Console.WriteLine(ex.ToString()); } // Build the title StringBuilder builder = new StringBuilder(); // Title builder.Append(Title); // Album if (!string.IsNullOrEmpty(Album)) { builder.AppendFormat(CultureInfo.InvariantCulture, " - {0}", Album); } // Artist if (!string.IsNullOrEmpty(Artists)) { builder.AppendFormat(CultureInfo.InvariantCulture, " - {0}", Artists); } Name = builder.ToString(); }