检查文件在C#中的媒体文件 [英] Check if file is a media file in C#

查看:96
本文介绍了检查文件在C#中的媒体文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种方法,可以告诉我,如果一个文件是图像,音频或视频文件。我能做到这一点的C#?

I need a method that could tell me if a file is image, audio or video file. Can I do this with C#?

推荐答案

这取决于你想要如何稳健它是。

It depends how robust you want it to be.

要做到这一点最简单的方法是检查的延伸,是这样的:

The simplest way to do it is to check the extension, like this:

static string[] mediaExtensions = {
    ".PNG", ".JPG", ".JPEG", ".BMP", ".GIF", //etc
    ".WAV", ".MID", ".MIDI", ".WMA", ".MP3", ".OGG", ".RMA", //etc
    ".AVI", ".MP4", ".DIVX", ".WMV", //etc
};

static bool IsMediaFile(string path) {
    return -1 != Array.IndexOf(mediaExtensions, Path.GetExtension(path).ToUpperInvariant());
}



修改:对于那些谁真的想LINQ,这里是:

EDIT: For those who really want LINQ, here it is:

return mediaExtensions.Contains(Path.GetExtension(path), StringComparer.OrdinalIgnoreCase);

请注意,一个文件的扩展名是不是其内容的可靠指标;任何人都可以重命名文件并更改其扩展名。

Note that a file's extension is not a reliable indicator of its content; anyone can rename a file and change its extension.

如果您还没有扩展名,或者,如果你不相信它,你可以阅读的开始文件,看它是否常见媒体格式文件的签名相匹配。

If you don't have the extension, or if you don't trust it, you can read the beginning of the file and see if it matches file signatures for common media formats.

这篇关于检查文件在C#中的媒体文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆