C#中获取音频文件时长

1.在项目中添加引入:COM组件的Microsoft Shell Controls And Automation

2.在引用中找到Shell32,点击右键,在属性中将“嵌入互操作类型”的值改为“false”

3.代码

 1 public static string GetVoiceTime(string voicePath)//voicePath为语音文件路径
 2         {
 3             string path = HttpContext.Current.Server.MapPath(voicePath);//将虚拟路径转换为绝对路径,这里必须用绝对路径!
 4             string dirName = Path.GetDirectoryName(path);//获取文件夹名称
 5             string voiceName = Path.GetFileName(path);//获取文件名
 6             FileInfo file = new FileInfo(path);
 7             ShellClass sh = new ShellClass();
 8             Folder dir = sh.NameSpace(dirName);
 9             FolderItem item = dir.ParseName(voiceName);
10             string songTime = dir.GetDetailsOf(item, 27);//win7参数为27
11             return songTime;
12         }