您的位置:首页 > 编程语言 > C#

C#判断文件格式

2014-01-04 18:18 190 查看
代码:

private string filetypeget(string name)
{
FileStream fs = new FileStream(@name, FileMode.Open, FileAccess.Read);
byte[] imagebytes = new byte[fs.Length];
BinaryReader br = new BinaryReader(fs);
imagebytes = br.ReadBytes(2);
string ss = "";
for (int i = 0; i < imagebytes.Length; i++)
{
ss += imagebytes[i];
}
return ss;
}
引用代码:

if ( filetypeget(addr)== "255216")
{
pictureaddr.Add(addr);
}


作用是判断该文件是否为jpg文件,其中addr为图片的路径。255216表示jpg。

各种文件格式文件头占的字节不一样,譬如:jpg,2个字节;png,8个字节;gif,6个字节。
你只需要改变读取的字节数,在判断就可以了。

.exe "7790"

.rar "8297"


.gif files: "7173"
    .ppt files: "208207"
    .log files: "4545"
    .txt files: "4545", "3232", "60104"
    .html files: "60104"
    .xml files: "6063"
    .xsd files: "6063"
    .xls files: "208207"
    .doc files: "208207"
    .msi files: "208207"
    .jpg files: "6677"
    .JPG files: "255216"
    .js files: "102117"
    .conf files: "3513"
    .jar files: "8075"

相关文章:http://www.cnblogs.com/gzlxm/archive/2010/05/25/1743296.html

相关文章:http://blog.163.com/china__xuhua/blog/static/199723169201111161478889/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c# 文件类型判断