C # Decision file format

Source: Internet
Author: User

Code:

        private string Filetypeget (string name)
        {
            FileStream fs = new FileStream (@name, FileMode.Open, FileAccess.Read) ;
            byte[] imagebytes = new Byte[fs. Length];
            BinaryReader br = new BinaryReader (fs);
            Imagebytes = Br. Readbytes (2);
            string ss = "";
            for (int i = 0; i < imagebytes. Length; i++)
            {
                SS = Imagebytes[i];
            return SS;
        }
Reference code:

                if (filetypeget (addr) = = "255216")
                {
                    pictureaddr. ADD (addr);
                }

The role is to determine whether the file is a JPG file, where addr is the path to the picture. 255216 represents JPG.

Each file format file header occupies a different byte, such as: jpg,2 byte; png,8 byte; gif,6 byte.
You just need to change the number of bytes read in the judgment on it.
. exe "7790"
. rar "8297"

GIF files: "7173"
. ppt files: "208207"
. log files: "4545"
. txt files: "4545", "3232", "60104"
. html files: "60104"
. xml files: "6063"
. xsd files: "6063"
. xls files: "208207"
. doc files: "208207"
. msi files: "208207"
. jpg files: "6677"
. JPG files: "255216"
. js files: "102117"
. conf files: "3513"
. jar files: "8075"

Related article: http://www.cnblogs.com/gzlxm/archive/2010/05/25/1743296.html

Related article: http://blog.163.com/china__xuhua/blog/static/199723169201111161478889/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.