2

I would like to determine real file extension.

example :

   file = "test.fakeExt"
    // but the real extention is .exe // for security reason I wish to avoid using it! 

How can I do that?

Alex K.
  • 171,639
  • 30
  • 264
  • 288
Azzeddine
  • 176
  • 2
  • 9
  • please post more code for us to help. how do u get the filename? in c#, string[] s= file.split('.'); string ext = s[s.Length-1]; – Arif YILMAZ Aug 02 '13 at 14:19
  • What do you mean by the 'real extension'? If it is a link it will have a .lnk extension – Matt Wilko Aug 02 '13 at 14:21
  • 2
    You could use the FileInfo class. Handling with strings to get filenames is a bad idea. – Rene Niediek Aug 02 '13 at 14:24
  • I mean that I have some fake extention, some ppl send me fraud files with (.exe) but they put a fake ext (.txt). So I would like to detecte real extention for security reason! I wish to get MIME type of the file – Azzeddine Aug 02 '13 at 14:26
  • possible duplicate of [How to find if a file is an exe?](http://stackoverflow.com/questions/2863683/how-to-find-if-a-file-is-an-exe) – Matt Wilko Aug 02 '13 at 14:28
  • It might corrupt file (virus infected !) ... – matzone Aug 02 '13 at 14:29
  • It won't do anything malicious unless you change the extension yourself and how would you know what to change it to? – Matt Wilko Aug 02 '13 at 14:31

2 Answers2

1

If you want to determine the extension you could use findmimefromdata. It looks at the first part of the file to determine what type of file it is.

FindMimeFromData function

Sample code

traxs
  • 306
  • 1
  • 5
1

The first two bytes of an .exe file are allways 'MZ'.

So you could read the binary file, and see if the first two bytes are MZ, then you know it's an .exe file...

Dennis
  • 1,528
  • 2
  • 16
  • 31