Detect file type?

How would I detect the filetype of a file?

Thanks,

paoloTheCool

[83 byte] By [paoloTheCool] at [2008-1-10]
# 1
if u mean find out the extension, like .dll, .jpg, .mp3, etc. then just use:
System.IO.Path.GetExtension(string path);
or
System.IO.FileInfo f = new System.IO.FileInfo("C\\:blah.txt");
return f.Extension;
CameronKloot at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...
# 2

Getting extension and finding out file type is completly different. I could save a file with a ".jpeg" when it is really a ".ico" file.

Thanks,

paoloTheCool

paoloTheCool at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...
# 3
there isnt a way to access the "file type" by those standerds. .exe = App file. .jpg, .bmp, .png = Image file. .mp3, .mp4, .ogg = audio file. they only way to find out the file type is by the extension, im prty sure about that.
CameronKloot at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...
# 4

For files that contain a header (e. g. .bmp, .wav), you can open the file and examine it to determine it. For files that don't have a header (e. g. .txt), then you will have to come up with some other way to do it.

Usually the file's extension identifies the file type. If you don't trust that, then you will have to examine the file.

Chris

ChrisDunaway at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...
# 5
Let's not even think about what if you're trying to examine a encrypted file (without having it decrypted by the file system for you).

The only two ways is to go by header (or footer in some cases) or by file extention. I could go create my own file type with a specific format, no header, and call it a sff. How would you expect to know what this is? It could be something like a xaml file where analysis of it (ignoring any headers) would tell you it might be a text file... or a xml file... or a xaml file...

I think you should stick to file extention, then check file types with tag information for the prescense of the proper tagging. If the file extention fails, you can check for tags. However if the tag checks fail treat it as unknown (my only slight clarification of Chris' explanation).

What is your goal in checking for file type?

IsshouFuuraibou at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...
# 6

Ok...so I guess there's no easy method for accuratly detecting the file type easily. I'll just use extensions.

Thanks,

paoloTheCool

paoloTheCool at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...