C# 判断文件真实类型,不是通过扩展名来判断

238次阅读
没有评论

共计 912 个字符,预计需要花费 3 分钟才能阅读完成。

/// <summary>
/// 判断文件格式
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public static bool IsAllowedExtension(string filePath)
{FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
    BinaryReader reader = new BinaryReader(stream);
    string fileclass = "";
    // byte buffer;
    try
    {//buffer = reader.ReadByte();
        //fileclass = buffer.ToString();
        //buffer = reader.ReadByte();
        //fileclass += buffer.ToString();

        for (int i = 0; i < 2; i++)
        {fileclass += reader.ReadByte().ToString();}
    }
    catch (Exception)
    {throw;}

    if (fileclass == "255216")
    {return true;}
    else
    {return false;}

    /* 文件扩展名说明
    * 255216 jpg
    * 208207 doc xls ppt wps
    * 8075 docx pptx xlsx zip
    * 5150 txt
    * 8297 rar
    * 7790 exe
    * 3780 pdf      
    * 
    * 4946/104116 txt
    * 7173        gif 
    * 255216      jpg
    * 13780       png
    * 6677        bmp
    * 239187      txt,aspx,asp,sql
    * 208207      xls.doc.ppt
    * 6063        xml
    * 6033        htm,html
    * 4742        js
    * 8075        xlsx,zip,pptx,mmap,zip
    * 8297        rar   
    * 01          accdb,mdb
    * 7790        exe,dll
    * 5666        psd 
    * 255254      rdp 
    * 10056       bt 种子 
    * 64101       bat 
    * 4059        sgf    
    */

}

转自:https://www.cnblogs.com/babycool/p/3531696.html

正文完
 0
flames
版权声明:本文于2022-03-11转载自酷小孩,共计912字。
转载提示:此文章非本站原创文章,若需转载请联系原作者获得转载授权。
评论(没有评论)