Search code examples
c#.net-corewindows-api-code-pack

Reading extended file properties with .NET Core


I'm trying to port a .NET Framework application to .NET Core. That application reads the extended properties of a file, like one can see when right clicking on a file in the File Explorer:

enter image description here

In my 4.7.2 Framework application, the code looked like this:

using Microsoft.WindowsAPICodePack.Shell;
      
var shellFile = ShellFile.FromFilePath(file.FullName);
var title = shellFile.Properties.System.Title.Value;
var albumTitle = shellFile.Properties.System.Music.AlbumTitle.Value;

My project referenced the Shell code like this:

enter image description here

What do I need to do in my .NET Core application to get access to the code of Microsoft.WindowsAPICodePack.Shell ? Or does .NET Core offer another way how to read those file properties ?

Before you mark this question as dupplicate

I know that a similar question was asked already: Obtaining file extended properties in .Net Core

However, this question was asked 3 years ago, when .NET Core was very young. The 2 answers do not explain how to read the extended properties of a file. It seems the people answering did not understand the difference between extended file properties and the file properties one can get from FileInfo. Maybe the question was not clear enough ? I also hope that .NET Core has improved enough that such legacy functionality can get used again.


Solution

  • I don't think these are file attributes. I guess, it is MP3 metadata stored in ID3 tags.

    You are using .NET Framework NuGet package WindowsAPICodePack-Shell that can read such metadata.

    Option #1

    I couldn't find a .NET Core version of the original package. But I found an unofficial .NET Core fork of the library: Microsoft-WindowsAPICodePack-Shell (it's not authored by Microsoft).

    Option #2

    For .NET Core you can install the TagLibSharp NuGet package. And then you just read metadata like this:

    var file = new FileInfo("track.mp3");
    var tagLibFile = TagLib.File.Create(file.Name);
    var title = tagLibFile.Tag.Title;
    var album = tagLibFile.Tag.Album;
    var albumArtist = tagLibFile.Tag.AlbumArtists;
    var genres = tagLibFile.Tag.JoinedGenres;
    var length = tagLibFile.Properties.Duration;