美文网首页C#与.net
C#判断文件的真实类型(非扩展名判断)

C#判断文件的真实类型(非扩展名判断)

作者: NewForMe | 来源:发表于2019-08-28 14:35 被阅读0次
    public static void CheckTrueFileName()
    {
        string path = @"D:\Sheet1.doc";
        System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read);
        System.IO.BinaryReader reader = new System.IO.BinaryReader(fs);
        string bx = ""; //文件编码
        byte buffer;
        try
        {
            for(int i=0;i<2;i++)
              {
                  bx +=reader.ReadByte().ToString();
              }
            //buffer = reader.ReadByte();
            //bx = buffer.ToString();
            //buffer = reader.ReadByte();
            //bx += buffer.ToString();
        }
        catch (Exception exc)
        {
            Console.WriteLine(exc.Message);
        }
        reader.Close();
        fs.Close();
        //真实的文件类型
        Console.WriteLine(bx);
        //文件名,包括格式
        Console.WriteLine(System.IO.Path.GetFileName(path));
        //文件名, 不包括格式
        Console.WriteLine(System.IO.Path.GetFileNameWithoutExtension(path));
        //文件格式
        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,
        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,
        PPT=208207,
        ZIP = 8075,
        DOCX = 8075,
        XLSX = 8075,
        PPTX=8075,
        MMAP=8075,
        CSV=4944
    }
    

    相关文章

      网友评论

        本文标题:C#判断文件的真实类型(非扩展名判断)

        本文链接:https://www.haomeiwen.com/subject/vfnpectx.html