怎么通过文件头判断文件类型.

TossShinHwa 2010-02-01 03:12:20
RT.

求详细方法.

是不是要用COM?
...全文
912 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
捡破烂攻城狮 2011-10-23
  • 打赏
  • 举报
回复
看看这个
http://hi.baidu.com/passedbylove/blog/item/e69351ca22d28957f21fe7e8.html
cgp2008 2010-10-07
  • 打赏
  • 举报
回复
看看先
DayDayUpCQ 2010-09-11
  • 打赏
  • 举报
回复
谢谢!
alanhuqi 2010-06-10
  • 打赏
  • 举报
回复
Mark 学习
Sillen 2010-06-09
  • 打赏
  • 举报
回复
Mark 学习
lzsh0622 2010-02-01
  • 打赏
  • 举报
回复

FileStream fs = new FileStream(txb_File.Text, FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(fs);
byte[] b = new byte[30];
MemoryStream ms = new MemoryStream();
r.Read(b, 0, b.Length);
// ...
r.Close();
fs.Close();

缭绕飘渺 2010-02-01
  • 打赏
  • 举报
回复
学习了
第一次知道这么判断文件的
lzsh0622 2010-02-01
  • 打赏
  • 举报
回复
按二进拷制方式,读出文件的前几个字节比较,这种方法并不严格。

JPEG (jpg),文件头:FFD8FF
PNG (png),文件头:89504E47
GIF (gif),文件头:47494638
TIFF (tif),文件头:49492A00
Windows Bitmap (bmp),文件头:424D
CAD (dwg),文件头:41433130
Adobe Photoshop (psd),文件头:38425053
Rich Text Format (rtf),文件头:7B5C727466
XML (xml),文件头:3C3F786D6C
HTML (html),文件头:68746D6C3E
Email [thorough only] (eml),文件头:44656C69766572792D646174653A
Outlook Express (dbx),文件头:CFAD12FEC5FD746F
Outlook (pst),文件头:2142444E
MS Word/Excel (xls.or.doc),文件头:D0CF11E0
MS Access (mdb),文件头:5374616E64617264204A
WordPerfect (wpd),文件头:FF575043
Postscript (eps.or.ps),文件头:252150532D41646F6265
Adobe Acrobat (pdf),文件头:255044462D312E
Quicken (qdf),文件头:AC9EBD8F
Windows Password (pwl),文件头:E3828596
ZIP Archive (zip),文件头:504B0304
RAR Archive (rar),文件头:52617221
Wave (wav),文件头:57415645
AVI (avi),文件头:41564920
Real Audio (ram),文件头:2E7261FD
Real Media (rm),文件头:2E524D46
MPEG (mpg),文件头:000001BA
MPEG (mpg),文件头:000001B3
Quicktime (mov),文件头:6D6F6F76
Windows Media (asf),文件头:3026B2758E66CF11
MIDI (mid),文件头:4D546864
ck11926375 2010-02-01
  • 打赏
  • 举报
回复
楼主到底是要判断哪些文件?
problc 2010-02-01
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 ck11926375 的回复:]
C# codebool xx=false;//default sFileName is not Exe or Dll File System.IO.FileStream fs=new System.IO.FileStream(sFileName,System.IO.FileMode.Open,System.IO.FileAccess.Read);
System.IO.BinaryReader r=new System.IO.BinaryReader(fs);string bx="";byte buffer;try
{
buffer=r.ReadByte();
bx=buffer.ToString();
buffer=r.ReadByte();
bx+=buffer.ToString();

}catch (Exception exc)
{
Console.WriteLine(exc.Message);
}
r.Close();
fs.Close();if (bx=="7790"||bx=="8297"||bx=="8075")//7790:exe,8297:rar,8075:pk {
xx=true;
}
Console.WriteLine(bx);return xx;
                dll:MZ
                exe:MZ
                rar:Rar
                zip:PK
[/Quote]
up,网上还可以找到其他的一些常用前缀
cjnkd 2010-02-01
  • 打赏
  • 举报
回复
帮顶一个
ck11926375 2010-02-01
  • 打赏
  • 举报
回复
bool xx=false;  //default sFileName is not Exe or Dll File
System.IO.FileStream fs=new System.IO.FileStream(sFileName,System.IO.FileMode.Open,System.IO.FileAccess.Read);
System.IO.BinaryReader r=new System.IO.BinaryReader(fs);
string bx="";
byte buffer;
try
{
buffer=r.ReadByte();
bx=buffer.ToString();
buffer=r.ReadByte();
bx+=buffer.ToString();

}
catch (Exception exc)
{
Console.WriteLine(exc.Message);
}
r.Close();
fs.Close();
if (bx=="7790"||bx=="8297"||bx=="8075")//7790:exe,8297:rar,8075:pk
{
xx=true;
}
Console.WriteLine(bx);
return xx;

dll:MZ
exe:MZ
rar:Rar
zip:PK
cvbnm582 2010-02-01
  • 打赏
  • 举报
回复
com已成为传说
qqzeng-ip 2010-02-01
  • 打赏
  • 举报
回复


public static string GetContentTypeByExtName(string extName)
{
switch (extName.ToLower())
{
case ".001":
return "application/x-001";

case ".301":
return "application/x-301";

case ".323":
return "text/h323";

case ".906":
return "application/x-906";

case ".907":
return "drawing/907";


.........

....
return "application/x-wrk";

case ".ws":
return "application/x-ws";

case ".ws2":
return "application/x-ws";

case ".wsc":
return "text/scriptlet";

case ".wsdl":
return "text/xml";

case ".wvx":
return "video/x-ms-wvx";

case ".xdp":
return "application/vnd.adobe.xdp";

case ".xdr":
return "text/xml";

case ".xfd":
return "application/vnd.adobe.xfd";

case ".xfdf":
return "application/vnd.adobe.xfdf";

case ".xhtml":
return "text/html";

case ".xls":
return "application/x-xls";

case ".xlw":
return "application/x-xlw";

case ".xml":
return "text/xml";

case ".xpl":
return "audio/scpls";

case ".xq":
return "text/xml";

case ".xql":
return "text/xml";

case ".xquery":
return "text/xml";

case ".xsd":
return "text/xml";

case ".xsl":
return "text/xml";

case ".xslt":
return "text/xml";

case ".xwd":
return "application/x-xwd";

case ".x_b":
return "application/x-x_b";

case ".x_t":
return "application/x-x_t";
}
return "application/octet-stream";
}
toxuecheng111 2010-02-01
  • 打赏
  • 举报
回复
wait........
qlzf11140820 2010-02-01
  • 打赏
  • 举报
回复
文件头 =? 头文件
  • 打赏
  • 举报
回复
另外,COM也不是什么奇迹
  • 打赏
  • 举报
回复
你说的是什么文件啊?图像?文档?还是PE??????
我的心里充满了无数的问号
TossShinHwa 2010-02-01
  • 打赏
  • 举报
回复
PS:是C/S的
hyfzz123 2010-02-01
  • 打赏
  • 举报
回复
zhichi

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧