C#判斷文件的真實類型(非擴展名判斷)

public static void CheckFileType()
{
    string path = @"D:\word.doc";
    System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read);
    System.IO.BinaryReader r = new System.IO.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);
    }
    r.Close();
    fs.Close();
    //真實的文件類型
    Console.WriteLine(bx);
    //文件格式
    Console.WriteLine(System.IO.Path.GetExtension(path));
    Console.ReadLine();
}






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,
}
調用實例:string txt= CheckFileType();

if(txt=="")
相關文章
相關標籤/搜索