UWP开发细节记录:判断文件类型

时间:2023-03-10 02:27:27
UWP开发细节记录:判断文件类型

StorageFile.ContentType 属性,是 string 类型,用来表示文件内容的 MIME 类型。例如,音乐文件可能有 "audio/mpeg" MIME 类型。(MSDN)

MIME 类型的定义可以下面的链接找到:

MIME Types - http://blogs.msdn.com/b/jaimer/archive/2008/01/04/mime-types.aspx

MIME 参考手册 - http://www.w3school.com.cn/media/media_mimeref.asp

随便摘录了几个如下:

.mov

video/quicktime

.movie

video/x-sgi-movie

.mp2

video/mpeg

.mp3

audio/mpeg

.mpa

video/mpeg

.mpe

video/mpeg

.mpeg

video/mpeg

.mpg

video/mpeg

.mpp

application/vnd.ms-project

所以可以简单的通过查找字符串判断文件的类型,如视频文件/图像文件等。

StorageFile file = await openPicker.PickSingleFileAsync();

if (file .ContentType.IndexOf("video/") == )
{
// 视频文件
}
else if (file .ContentType.IndexOf("image/") == )
{
// 图像文件
}

不过,经测试,这个类型只是简单的通过文件扩展名判断的,并不可靠

另外,MSDN上备注说 WP8 系统不实现此 API ,调用会引发异常。未说明 Win10 Mobile 中是否可用。