当前位置:编程学习 > C#/ASP.NET >>

请教一个 FileStream 检查文件类型的问题,谢谢!

我用WINFORM做了一个应用程序,但不知道用什么方法可以检查文件的真正类型,别人说是用FileStream类,但我不知道具体应该怎么做,谢谢!

主要检查文件的类型,如:txt,exe,rar,doc,docx,html,cs,swf,jpg等

不能通过扩展名来检查,因为这样是别人可以改扩展名,不能借助WEB的HTTPOSTFILE类来检查!

     private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.OpenFileDialog openFile = new OpenFileDialog();
            openFile.Filter = "All Files|*.*";
            if (openFile.ShowDialog() == DialogResult.OK)
            {
                this.StatusText.Text = openFile.FileName;
                this.StatusText.Visible = true;
                String stResult = "";
                FileStream fs = new FileStream(this.StatusText.Text, FileMode.Open);
                BinaryReader br = new BinaryReader(fs);
                byte buffer;
                buffer = br.ReadByte();
                stResult = buffer.ToString();
                buffer = br.ReadByte();
                stResult += buffer.ToString();
                this.StatusText.Text = stResult;
                br.Close();
                fs.Close();
                fs.Dispose();
            }
        }

这样检查文件类型有些不准确,JPG,EXE,RAR是准确的(根据stResult来判断),但txt、html这类文件就不确定,判断不出来! --------------------编程问答-------------------- 在线等! --------------------编程问答-------------------- MediaInfo呢?用这个开源类检查类型? --------------------编程问答-------------------- 只要能检查出真正文件类型就好,谢谢

--------------------编程问答-------------------- 不能用扩展名就不会了,去google --------------------编程问答-------------------- 不一定要用FileStream类,只要能实现检查真正的文件类型就好。 --------------------编程问答-------------------- unix 的 file 命令靠 magic number 来判断大多数文件类型, 

C# 也有这样的.

可以参考一下 
A small Content Detection Library
http://www.codeproject.com/KB/cs/ContentDetectorLib.aspx --------------------编程问答-------------------- 读两个字节,判断文件扩展名说明。 
你需要的其他文件扩展名代码要去网上搜搜,我没找到 
真正做的时候,fileType 定义一个枚举就可以了 


private bool IsAllowedExtension(HttpPostedFile hifile)
        {
            bool ret = false;

            System.IO.FileStream fs = new System.IO.FileStream(hifile.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
            System.IO.BinaryReader r = new System.IO.BinaryReader(fs);
            string fileclass = "";
            byte buffer;
            try
            {
                buffer = r.ReadByte();
                fileclass = buffer.ToString();
                buffer = r.ReadByte();
                fileclass += buffer.ToString();
            }
            catch
            {
                return false;
            }
            r.Close();
            fs.Close();
            /*文件扩展名说明
             7173 gif 
            255216 jpg 
             7790 exe dll 
            00 ani--ico--cur 
            7783 
           255254 --ini 
           9146 -- ini 
           5866 
             6395 hlp 
          8269 reg 
    70105 log 
    205168 
    7384 chm 
    5549 txt 
    117115 txt 
    5450 txt 
    5666 psd 
             *7173        gif 
             *255216      jpg
             *13780       png
             *6677        bmp
             *239187      txt,aspx,asp,sql
             *208207      xls.doc.ppt
             *6063        xml
             *6033        htm,html
             *4742        js
             *8075        xlsx,zip,pptx,mmap,zip
             *8297        rar   
             *01          accdb,mdb
             *7790        exe,dll           
             *5666        psd 
             *255254      rdp 
             *10056       bt种子 
             *64101       bat 
             */


            String[] fileType = { "255216", "7173", "6677", "13780", "8297", "5549", "870", "87111", "8075" };

            for (int i = 0; i < fileType.Length; i++)
            {
                if (fileclass == fileType[i])
                {
                    ret = true;
                    break;
                }
            }
            return ret;       
        }


--------------------编程问答-------------------- Hide1984 这个判断txt、html类型文件不准确 --------------------编程问答-------------------- 关注下 --------------------编程问答-------------------- --------------------编程问答-------------------- 7楼的可行 --------------------编程问答-------------------- 不行。 --------------------编程问答-------------------- 既然,可以改后缀名,那别的都可以改了。
你控制的了吗?
改的方法比识别的方法多 --------------------编程问答-------------------- 随便你怎么改,文件内容就可以检查出是什么格式的 --------------------编程问答-------------------- 数据内容总不会变吧 --------------------编程问答-------------------- 顶
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,