c# 判断文件类型

        //二进制下的文件头。主要是前两位
         public enum FileExtensione
        {
            JPG = 255216,
            GIF = 7173,
            BMP = 6677,
            TIFF = 7373,
            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,
        }
         static CommonTools()
        {
            FileExtension.Add("255216", "JPG");
            FileExtension.Add("7173", "GIF");
            FileExtension.Add("6677", "BMP");
            FileExtension.Add("7373", "TIFF");
            FileExtension.Add("13780", "PNG");
            FileExtension.Add("3780", "PDF");
            FileExtension.Add("208207", "DOC");
        }
        static Dictionary<string, string> FileExtension = new Dictionary<string,string>();        


        /// <summary>
        /// 读文件 byte
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <returns></returns>
        public static byte[] readFileByte(string fileName)
        {
            using (FileStream fsRead = new FileStream(fileName, FileMode.Open))
            {
                int fsLen = (int)fsRead.Length;
                byte[] heByte = new byte[fsLen];
                int r = fsRead.Read(heByte, 0, heByte.Length);
                if (fsRead != null)
                    fsRead.Close();
                return heByte;
            }
        }
         /// <summary>
        /// 检测文件类型
        /// </summary>
        /// <param name="fullpath">文件完整路径</param>
        /// <returns></returns>
        public static string CheckImgType(string fullpath)
        {
            try
            {
                byte[] byData = readFileByte(fullpath);

                try
                {
                    string typecode = byData[0].ToString() + byData[1].ToString();

                    if (FileExtension.ContainsKey(typecode))
                    {
                        return FileExtension[typecode];
                    }
                    else
                    {
                        return "";
                    }
                }
                catch (Exception ex)
                {
                    //SpecialTools.ShowMsg("code:0xFF07895 获取文件类型出现未知错误" + ex.Message);
                }
            }
            catch (Exception e)
            {
                //SpecialTools.ShowMsg("code:0xFF07896 获取文件类型出现未知错误" + e.Message);
            }
            return "";
        }

可能这个种方式有缺陷,但是应该是比较好的判断方式。因为网上很多人说不一定取前两个字节,有的文件可能不止,至于原因我也不知道为啥。如果有朋友知道麻烦留个言,让我学习下

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值