Go Back   Computer Forums > General Computing > Programming
Join Computer forums Today

Thread Tools Search this Thread Display Modes
Old 01-28-2009, 05:34 PM   #1
Baseband Member
Join Date: Nov 2008
Posts: 91
Default identifying file types

Within a C# program, is there any way to tell if a file is a text file or not? I mean a real way, not just basing your conclusion on a file name extension.

BobLewiston is offline   Reply With Quote
Old 01-29-2009, 08:03 AM   #2
In Runtime
Daeva's Avatar
Join Date: Dec 2005
Posts: 407
Send a message via AIM to Daeva Send a message via MSN to Daeva Send a message via Yahoo to Daeva
Default Re: identifying file types

That depends on what level of complexity you want to involve in your program. Even windows doesn't get all bit-level on you to determine the file type, it simply uses the extension and then maps that extension to an application via the registry. You could develop patterns for certain files or attempt to convert them to base64 and look for certain strings of data to determine the type, for example: the base64 of a jpg typically starts with "/9j/"
here is an example of part of a base64 string for a jpg:
The complete base64 is quite large, and it would be difficult to parse through any further than the first several characters.

I'm not about other files types, but unless the difference is REALLY important, I would just stick with the extension. Alternatively you could check for multiple extensions, like if someone attempted to circumvent a file upload like renaming bad.exe to bad.exe.tmp or bad.tmp. Hope this helps.
**Official Self-proclaimed glorified excessive (insert additional adjectives here) post editor/modifier.
Edit = Best feature ever
Daeva is offline   Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

All times are GMT -5. The time now is 08:41 PM.

Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2016, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0