判断文件的原始格式
很简单当笔记用
1 public static void FindFilename(string filePath) 2 { 3 try 4 { 5 string fileExtension = null; 6 using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read)) 7 { 8 using (BinaryReader br = new BinaryReader(fs)) 9 { 10 byte[] buffer = br.ReadBytes(2); 11 for (int i = 0; i < buffer.Length; i++) 12 { 13 fileExtension += buffer[i].ToString(); 14 } 15 if (Convert.ToInt32(fileExtension)==(int)Extension.XLS) 16 { 17 Console.WriteLine("TRUE"); 18 } 19 Console.Write(fileExtension); 20 } 21 } 22 23 } 24 catch (Exception ex) 25 { 26 Console.WriteLine(ex.ToString()); 27 } 28 }
枚举:
public enum Extension { 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, }
直接判断就OK 了,我判断的是xls,不同的文件类型,读取的字节也不一样!