利用 C# ShellClass 获取 MP3 文件时长

代码:

using System;
using System.IO;
using Shell32;

namespace GetMP3Duration
{
    class Program
    {
        static void Main(string[] args)
        {
            // 获取 MP3 文件的路径
            string filePath = @"C:pathtomp3.mp3";

            // 创建 ShellClass 实例
            ShellClass shell = new ShellClass();

            // 获取文件的 IShellItem2 接口
            IShellItem2 item = shell.NameSpace(filePath);

            // 从 IShellItem2 接口中获取文件的时长
            double duration = (double)item.Properties.Item("System.Media.Duration").Value;

            // 将时长转换为秒
            duration /= 10000000;

            // 打印文件时长
            Console.WriteLine($"文件时长:{duration:0.00} 秒");
        }
    }
}
zip 文件大小:15.41KB