C# 获取mp3文件的歌曲时间长度
using Shell32;
using System.Text.RegularExpressions;
2、添加COM组件
Microsoft Shell Controls And Automation
string dirName = Path.GetDirectoryName(SongPath);
string SongName = Path.GetFileName(SongPath);//获得歌曲名称
FileInfo fInfo = new FileInfo(SongPath);
ShellClass sh = new ShellClass();
Folder dir = sh.NameSpace(dirName);
FolderItem item = dir.ParseName(SongName);
string SongTime = Regex.Match(dir.GetDetailsOf(item, -1),"\\d:\\d{2}:\\d{2}").Value;//获取歌曲时间
注意:引入的Interop.Shell32.dll默认版本为1.0的,发布到服务器上有时会报:“Unable to cast COM object of type 'Shell32.ShellClass' to interface type 'Shell32.IShellDispatch5'.”的错,需要下载1.2的版本才能正常使用。
1.2版本下载地址:http://download.csdn.net/download/u013529927/8812075