C# 获取媒体文件播放时长
引用:
Interop.Shell32.dll
方法:
1 /// <summary> 2 /// 获取媒体文件播放时长 3 /// </summary> 4 /// <param name="path">媒体文件路径</param> 5 /// <returns></returns> 6 public static string GetMediaTimeLen(string path) 7 { 8 try 9 { 10 Shell32.Shell shell = new Shell32.Shell(); 11 12 //文件路径 13 14 Shell32.Folder folder = shell.NameSpace(path.Substring(0, path.LastIndexOf("\\"))); 15 16 //文件名称 17 18 Shell32.FolderItem folderitem = folder.ParseName(path.Substring(path.LastIndexOf("\\") + 1)); 19 20 if (Environment.OSVersion.Version.Major >= 6) 21 { 22 23 return folder.GetDetailsOf(folderitem, 27); 24 25 } 26 27 else 28 { 29 30 return folder.GetDetailsOf(folderitem, 21); 31 32 } 33 } 34 catch (Exception ex) 35 { 36 return null; 37 } 38 }
调用:
1 string itemtimelen = GetMediaTimeLen(path); 2 DateTime t1 = DateTime.Parse(itemtimelen);