根据文件头判断文件类型
参考:255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar 其他类型应该网上能查到叭,,我这就懒得去找了
1 public static bool IsPicture(string filePath) 2 { 3 try 4 { 5 using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read)) 6 { 7 using (var reader = new BinaryReader(fs)) 8 { 9 string fileClass; 10 byte buffer; 11 buffer = reader.ReadByte(); 12 fileClass = buffer.ToString(); 13 buffer = reader.ReadByte(); 14 fileClass += buffer.ToString(); 15 reader.Close(); 16 fs.Close(); 17 //255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar 18 switch (fileClass) 19 { 20 case "255216": 21 case "7173": 22 case "13780": 23 case "6677": 24 return true; 25 default: 26 return false; 27 } 28 } 29 } 30 } 31 catch 32 { 33 return false; 34 } 35 }