c#中没有扩展名时如何检查文件类型 [英] How do you check a file type when there is no extension in c#

查看:332
本文介绍了c#中没有扩展名时如何检查文件类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当c#中没有扩展名时,如何检查文件类型

How do you check a file type when there is no extension in c#

例如,我有没有扩展名的文件,它们是.mp4或.flv格式(只是没有扩展名).我计划将这些视频文件转换为音频文件,但是在开始转换之前,我想确定文件类型.有没有办法在C#中做到这一点?

For instance, I have files with no extension, that are either .mp4 or .flv format (just no extension). I plan on converting these video files to audio files however I would like to determine the file type before I start converting it. Is there a way to do this in C#?

我在想,也许我可以将文件重命名为name.mp4,然后对该文件执行一些任务, A)成功,表示文件确实是.mp4,或者 B)失败,在这种情况下,我可以将其重命名为.flv

I was thinking that maybe I could just rename the file to name.mp4, then perform some task on the file that would either A) succeed, meaning that the file was indeed .mp4, or B) fail, in which case I could then rename it to .flv

然后将文件转换为适当的扩展名. c#中是否有本机进程可以查看.mp4属性或.flv属性?我不想将文件重命名为.mp4,然后在第三方应用程序(例如Windows Media Player)中将其打开,以查看是否正确命名了该文件.

then convert the file as the appropriate extension. Is there a native process in c# that can look at .mp4 properties or .flv properties? I do not want to rename the file to .mp4 and then open it in a third party application, such as Windows Media Player, in order to see if I named it correctly.

推荐答案

我听说过读取文件内容的前几个字节,并对文件的格式进行有根据的猜测.该链接似乎很有希望:

I've heard of reading the first few bytes of a file's contents and making an educated guess at the file's format. This link seems promising:

查看全文

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