.net 实现得到文件夹中的图片文件

来源:互联网 发布:加工中心g76编程实例 编辑:程序博客网 时间:2024/05/16 05:10

 public bool IsPicture(string filePath)//filePath是文件的完整路径
        {
            try
            {
                System.IO.FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                BinaryReader reader = new BinaryReader(fs);
                string fileClass;
                byte buffer;
                byte[] b = new byte[2];
                buffer = reader.ReadByte();
                b[0] = buffer;
                fileClass = buffer.ToString();
                buffer = reader.ReadByte();
                b[1] = buffer;
                fileClass += buffer.ToString();


                reader.Close();
                fs.Close();
                if (fileClass == "255216 ")//255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar
                {
                    System.Console.Write(filePath);
                    return true;
                }
                else if (fileClass == "6677")
                {
                    System.Console.Write(filePath);
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
        } 

原创粉丝点击