c#真正判断文件类型

//真正判断文件类型的关键函数
    public static bool IsAllowedExtension2(FileUpload hifile)
    {
        if (hifile != null)
        {
            System.IO.FileStream fs = new System.IO.FileStream(hifile.PostedFile.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
            System.IO.BinaryReader r = new System.IO.BinaryReader(fs);
            string fileclass = "";
            byte buffer;
            try
            {
                buffer = r.ReadByte();
                fileclass = buffer.ToString();
                buffer = r.ReadByte();
                fileclass += buffer.ToString();

            }
            catch
            {

            }
            r.Close();
            fs.Close();
            if (fileclass == "255216" || fileclass == "7173")//说明255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else { return false; }

    }

 

 

  

posted on 2015-03-20 21:41  飞舞的蒲公英  阅读(3899)  评论(0编辑  收藏  举报