通过文件头判断文件类型、后辍
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string path = @"E: b55b319ebc4b745d931e035cefc1e178a82156f"; using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read)) { using (BinaryReader r = new BinaryReader(fs)) { string bx = ""; byte buffer; try { buffer = r.ReadByte(); bx = buffer.ToString(); buffer = r.ReadByte(); bx += buffer.ToString(); } catch (Exception exc) { Console.WriteLine(exc.Message); } Console.WriteLine(bx); Console.ReadLine(); } } } } }
常见文件类型对应编码
JPG = 255216, GIF = 7173, BMP = 6677, PNG = 13780, COM = 7790, EXE = 7790, DLL = 7790, RAR = 8297, ZIP = 8075, XML = 6063, HTML = 6033, ASPX = 239187, CS = 117115, JS = 119105, TXT = 210187, SQL = 255254, BAT = 64101, BTSEED = 10056, RDP = 255254, PSD = 5666, PDF = 3780, CHM = 7384, LOG = 70105, REG = 8269, HLP = 6395, DOC = 208207, XLS = 208207, DOCX = 208207, XLSX = 208207
参考资料:http://baike.baidu.com/link?url=ou1rjPoTKCnvIghdTdaL-QdSpgCaVm9qETOMoqBJxcj43N2OGgcyymH2-EfAj27ecuwQhasGBHXey9C-qC7bmK