您的位置:首页 > 编程语言 > C#

C#判断文件类型

2011-04-28 09:43 260 查看
下面的函数可以判断是否为图片,里面的fileClass 你自己根据注释添加其它的类型

C# code
private bool IsPicture(string filePath)//filePath是文件的完整路径
{
try
{
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
{ return true; }
else { return false; }
}
catch
{ return false; }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c# buffer byte string exe