我们可以使用Microsoft shell对象dll来实现这一点。将shell32引用添加到引用中的项目,然后参考下面的代码。
[STAThread] // Add STAThread attribute here
public static string GetVideoDuration(string filepath)
{
string formatedduration = "";
// Create Shell32 Shell object
Shell shell = new ShellClass();
// Get the folder that contains the video file
Folder folder = shell.NameSpace(System.IO.Path.GetDirectoryName(filepath));
// Get the video file
FolderItem folderItem = folder.ParseName(System.IO.Path.GetFileName(filepath));
// Get the duration property
string duration = folder.GetDetailsOf(folderItem, 27); // 27 is the index of the "Duration" property
formatedduration = GetTimeFormat(duration);
return formatedduration;
}
调用上述方法,如下所示:
//Create thread for shellobject
Thread thread = new Thread(() => { VideoDuration = GetVideoDuration(Server.MapPath(fileURL)); });
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
thread.Abort();