2

im about to create a small tool, which recreates all tags on my mp3 files. Because they are in a mess, i want to remove all tags and recreate them with the correct values.

Doing so ive encountered the problem that im not able to set the tag values. But the problem is, that im not able to set the tags. I have the following code:

File tagLibFile = File.Create(filePath);
tagLibFile.RemoveTags(TagLib.TagTypes.AllTags);
tagLibFile.Tag.Album = album;
tagLibFile.Tag.AlbumArtists = artists.ToArray();
tagLibFile.Tag.Track = track;
tagLibFile.Tag.Title = title;
tagLibFile.Tag.TitleSort = titleSort;
...
tagLibFile.Save();

The file is read out correctly. Then the tags are removed. But after that setting the Tag does not work. The strings inside the tag are still null. I havent seen a method like "tagLibFile.SetTag(Tag t)". The Tag is only available as a getter, but not a setter.

After that ive added some Frames, but that doesent have the effect of setting the tags. Maybe im using it the wrong way? Hope you can help me out of this!

Kind regards,

SyLuS

SyLuS
  • 103
  • 1
  • 10

1 Answers1

2

I'm guessing that after removing tags, TagLib# (or TagLib, for that matter) does not create a new tag to hold information. However, when opening a file, it possibly does some checking and if the file doesn't have one, it creates a new tag.

Hence, as a workaround, you could save the file once after removing the tags, and then proceed to add new tag information.

File tagLibFile = File.Create(filePath);
tagLibFile.RemoveTags(TagLib.TagTypes.AllTags);
// Save the file once, so that Taglib Sharp takes care of creating any necessary tags when opening the file next time and dispose the file reference:
tagLibFile.Save();
tagLibFile.Dispose();

You can then proceed to editing the tags as you're already doing after opening the file again:

tagLibFile = File.Create(filePath);
tagLibFile.Tag.Album = album;
tagLibFile.Tag.AlbumArtists = artists.ToArray();
tagLibFile.Tag.Track = track;
tagLibFile.Tag.Title = title;
tagLibFile.Tag.TitleSort = titleSort;
// ...

Remember to save the file again, after you're done editing tags:

tagLibFile.Save();

I hope this helps. If you have any further questions, or the above code doesn't work still, feel free to comment. :)

Fᴀʀʜᴀɴ Aɴᴀᴍ
  • 6,131
  • 5
  • 31
  • 52