如何在Windows窗体应用程序中获取MP3文件的BPM属性

Ben*_*Ben 9 c# mp3 file-properties

我试图从MP3文件中获取BPM属性:

在此输入图像描述

我可以在这个问题中看到如何在Windows应用商店应用中执行此操作:

如何在Windows应用商店C#中阅读mp3文件的Beats-per-minute标签?

但无法看到如何Windows.Storage在Windows窗体应用程序中使用.(如果我理解正确,那是因为Windows.StorageUWP特有的.)

如何在Forms应用程序中阅读此内容?如果没有原生的东西,很高兴使用(希望是免费的)库.

Sim*_*ier 10

您可以使用Windows的Scriptable Shell Objects.item对象具有ShellFolderItem.ExtendedProperty方法

您所追求的属性是名为System.Music.BeatsPerMinute的官方Windows属性

所以,这里是你如何使用它(你不需要引用任何东西,这要归功于dynamicCOM对象的酷C#语法):

static void Main(string[] args)
{
    string path = @"C:\path\kilroy_was_here.mp3";

    // instantiate the Application object
    dynamic shell = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"));

    // get the folder and the child
    var folder = shell.NameSpace(Path.GetDirectoryName(path));
    var item = folder.ParseName(Path.GetFileName(path));

    // get the item's property by it's canonical name. doc says it's a string
    string bpm = item.ExtendedProperty("System.Music.BeatsPerMinute");
    Console.WriteLine(bpm);
}
Run Code Online (Sandbox Code Playgroud)