C # determine whether a file is a text file [reprinted]

Source: Internet
Author: User

Address: http://www.cnblogs.com/criedshy/archive/2010/05/24/1742918.html
 

When I fixed the bugs today, I encountered a problem about the format of the uploaded file. The system needs to upload the. txt,. CSV format, which can be filtered Based on the file suffix. But if the user modifies the suffix to cheat the system, how can this problem be solved? For example, if a.jpg format is changed to a.txt, my current program cannot recognize it. Although errors can be displayed in the background, this error is not defined on FS.

How can this problem be solved?

I checked many pieces of data on the Internet. All the big data files were read as a binary file, and I took a single character segment, which is 255216 for example. jpg. The Code is as follows:

/// <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 = 1, 13780,
SWF = 1, 6787,
RAR = 8297,
Zip = 8075,
-7z = 55122,
Validfile = 9999999
// 255216 JPG;

// 7173 GIF;

/// 6677 BMP,

// 13780 PNG;

/// 6787 SWF

// 7790 exe dll,

// 8297 RAR

// Zip 8075

// 55122 7z

// XML 6063

// Html 6033

// 239187 aspx

// 117115 CS

// JS 119105

/// 102100 txt

// 255254 SQL
}

After testing, we can find a good file in the format of. jpg. GIF, but the value returned for each. txt file is not. Obviously, this method cannot meet my needs.

Later I saw a file written in Delphi. Here is a very simple method: think of the given file as a non-type binary file, and then read every byte of the file in sequence, if the value of one byte in the file is equal to 0, the file is not a text file; otherwise, if no byte value in the file is 0, you can determine that the file is a text file. This is the principle. Let's take a look at how to program it in 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;

After translating it into C # code, it is like this:

/// <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 ();
}
}
}

 

After tests, I met my needs.

Attached test code:

 

Bool istextfile = utility. checkistextfile (this. openfile. filename );

If (istextfile)
{
This. richtxtcontent. appendtext (openfile. filename + "is a text file ");
}
Else
{
This. richtxtcontent. appendtext (openfile. filename + "is not a text file! ");
}

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.