Code:
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;
}
Reference code:
if (filetypeget (addr) = = "255216")
{
pictureaddr. ADD (addr);
}
The role is to determine whether the file is a JPG file, where addr is the path to the picture. 255216 represents JPG.
Each file format file header occupies a different byte, such as: jpg,2 byte; png,8 byte; gif,6 byte.
You just need to change the number of bytes read in the judgment on it.
. 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"
Related article: http://www.cnblogs.com/gzlxm/archive/2010/05/25/1743296.html
Related article: http://blog.163.com/china__xuhua/blog/static/199723169201111161478889/