1. 程式人生 > >C#獲取真實檔案型別

C#獲取真實檔案型別

public class FileHelper
{
    public enum FileExtension
    {
        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,
    }
    public static void CheckType()
    {
        string path = @"D:\我的文件\My Pictures\logo5.png";
        System.IO.FileStream fileStream = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read);
        System.IO.BinaryReader binaryReader = new System.IO.BinaryReader(fileStream);
        string realType = " ";
        byte buffer;
        try
        {
            buffer = binaryReader.ReadByte();
            realType= buffer.ToString();
            buffer = binaryReader.ReadByte();
            realType+= buffer.ToString();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        binaryReader.Close();
        fileStream.Close();
        Console.WriteLine("真實的檔案型別", realType);
        Console.WriteLine("物理檔名格式", System.IO.Path.GetExtension(path));
        Console.ReadLine();
    }
}