C# 判斷檔案是否文字檔【轉載】

來源:互聯網
上載者:User

原文地址:http://www.cnblogs.com/criedshy/archive/2010/05/24/1742918.html 
 

今天fix bugs時,碰到一個關於上傳檔案格式的問題。系統要求上傳.txt,.csv格式的,這個可以根據檔案尾碼名來過濾。但是如果使用者修改了尾碼名來欺騙系統的話又該怎麼解決?比如a.jpg格式的改成a.txt,我現在的程式就無法識別了,雖然在後台可以彈出錯誤,但這個錯誤已經不是FS上定義的錯誤了。

    怎麼解決呢?

    在網上查了好多資料,大部分都是通過將檔案讀成二進位流,取前兩個位元組判斷,比如.jpg的是255216.代碼如下:

 /// <summary>
        /// Checks the file is textfile or not.
        /// </summary>
        /// <param name="fileName">Name of the file.</param>
        /// <returns></returns>
        public static FileExtension CheckTextFile(string fileName)
        {
            FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
            string fileType = string.Empty; ;
            try
            {
                byte data = br.ReadByte();
                fileType += data.ToString();
                data = br.ReadByte();
                fileType += data.ToString();
                FileExtension extension;
                try
                {
                    extension = (FileExtension)Enum.Parse(typeof(FileExtension), fileType);
                }
                catch 
                { 

                    extension=FileExtension.VALIDFILE
                }
                return extension;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                    br.Close();
                }
            }
        }
    }
    public enum FileExtension
    {
        JPG = 255216,
        GIF = 7173,
        PNG = 13780,
        SWF = 6787,
        RAR = 8297,
        ZIP = 8075,
        _7Z = 55122,
        VALIDFILE=9999999
        // 255216 jpg;  

        // 7173 gif;  

        // 6677 bmp,  

        // 13780 png;  

        // 6787 swf  

        // 7790 exe dll,  

        // 8297 rar  

        // 8075 zip  

        // 55122 7z  

        // 6063 xml  

        // 6033 html  

        // 239187 aspx  

        // 117115 cs  

        // 119105 js  

        // 102100 txt  

        // 255254 sql   
     }   

   

   經過測試,可以很好的判斷.jpg、.gif格式的檔案,然而對於.txt檔案卻不是102100,每個.txt返回的值也不一樣。顯然,這個方法不能滿足我的需要。

      後來看到一個delphi寫的,這裡有一個很簡單的方法:把給定的那個檔案看作是無類型的二進位檔案,然後順序地讀出這個檔案的每一個位元組,如果檔案裡有一個位元組的值等於0,那麼這個檔案就不是文字檔;反之,如果這個檔案中沒有一個位元組的值是0的話,就可以判定這個檔案是文字檔了。這是原理,下面看看在Delphi 中怎樣編程來實現它-- 

 

function IsTextFile(FileName:string):boolean; 
var 
Fs:TFileStream; 
i,size:integer; 
IsTextFile:boolean; 
ByteData:Byte; 
begin 
if FileExists(FileName) then 
begin 
Fs:=TFileStream.Create(FileName,fmOpenRead); 
IsTextFile:=true; 
i:=0; 
size:=Fs.Size; 
While (i<size) and IsTextFile do 
begin 
Fs.Read(ByteData,1); 
IsTextFile:=ByteData<>0; 
inc(i) 
end; 
Fs.Free; 
Result:=IsTextFile 
end 
else 
Result:=false end; 

     我把它翻譯成C#代碼後是這樣的:

 /// <summary>
        /// Checks the file is textfile or not.
        /// </summary>
        /// <param name="fileName">Name of the file.</param>
        /// <returns></returns>
        public static bool CheckIsTextFile(string fileName)
        {
            FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            bool isTextFile=true;
            try
            {
                int i = 0;
                int length = (int)fs.Length;
                byte data;
                while (i < length && isTextFile)
                {
                    data = (byte)fs.ReadByte();
                    isTextFile = (data != 0);
                    i++;
                }
                return isTextFile;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                }
            }
        } 

 

後來經過測試,滿足了我的需求。

  附測試代碼:

 

               bool isTextFile = Utility.CheckIsTextFile(this.openFile.FileName);

                if (isTextFile)
                {
                    this.richTxtContent.AppendText(openFile.FileName + "是文字檔");
                }
                else
                {
                    this.richTxtContent.AppendText(openFile.FileName + "不是文字檔!");
                }

 

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.