C# 根據文件頭判斷文件類型


 

 

根據文件頭判斷文件類型

參考: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         }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM