Mime Type of a file

T

tinman77

Thanks in advance for everyone's help...

My problem is that more often than I would like, I get attachments
that for some reason or another do not include an extension. What I
want to do is create a C# app that based on a file on my hard drive
that I specify, will spit back to me the mime type. I can do the rest
from there.

I have looked, and looked and looked a little more for a way to
determine the type of a file that does not rely on the extension. So
far I have come up with next to nothing - a couple libraries that do
this but that's not what I'm looking for. I don't want to use a whole
extra just to find out what kind of file I have.

To clarify what I'm looking for, PHP has similar functionality using
the FileInfo class. Simple, easy and to the point. Does nothing like
this exist in C#? I have looked at different class on MSDN and
searched Google relentlessly but, I got nothing. I read something
about the Attachment class but I was having problems following the
documentation I found for it.

I have provided a general outline for the code I am looking for. Feel
free to correct me if my request is semi-outrageous.

myFile = "C:\screw-the-extension"
myFileObject = new File(myFile)
print myFileObject.MimeType

Any help is more than appreciated.
 
T

tinman77

Thanks in advance for everyone's help...

My problem is that more often than I would like, I get attachments
that for some reason or another do not include an extension. What I
want to do is create a C# app that based on a file on my hard drive
that I specify, will spit back to me the mime type. I can do the rest
from there.

I have looked, and looked and looked a little more for a way to
determine the type of a file that does not rely on the extension. So
far I have come up with next to nothing - a couple libraries that do
this but that's not what I'm looking for. I don't want to use a whole
extra just to find out what kind of file I have.

To clarify what I'm looking for, PHP has similar functionality using
the FileInfo class. Simple, easy and to the point. Does nothing like
this exist in C#? I have looked at different class on MSDN and
searched Google relentlessly but, I got nothing. I read something
about the Attachment class but I was having problems following the
documentation I found for it.

I have provided a general outline for the code I am looking for. Feel
free to correct me if my request is semi-outrageous.

myFile = "C:\screw-the-extension"
myFileObject = new File(myFile)
print myFileObject.MimeType

Any help is more than appreciated.

Well apparently nothing like this exists. However I found a class that
will return the mime type of a file. Source and source code below.
using System;
using System.IO;
using System.Runtime.InteropServices;

/// <summary>
/// Utility class contains static method checkType to determine Mime
Type
/// Found at http://blogs.msdn.com/bwaldron/archive/2005/01/04/346547.aspx
/// </summary>
public class MimeTypeUtil
{
[DllImport(@"urlmon.dll", CharSet = CharSet.Auto)]

private extern static System.UInt32 FindMimeFromData(
System.UInt32 pBC,
[MarshalAs(UnmanagedType.LPStr)] System.String pwzUrl,
[MarshalAs(UnmanagedType.LPArray)] byte[] pBuffer,
System.UInt32 cbSize,
[MarshalAs(UnmanagedType.LPStr)] System.String
pwzMimeProposed,
System.UInt32 dwMimeFlags,
out System.UInt32 ppwzMimeOut,
System.UInt32 dwReserved);

public static string CheckType(string filePath)
{
byte[] buffer = new byte[256];

// grab the first 256 bytes on the file
using (FileStream fileStream = new FileStream(filePath,
FileMode.Open))
{
if (fileStream.Length >= 256)
{
fileStream.Read(buffer, 0, 256);
}
else
{
fileStream.Read(buffer, 0, (int)fileStream.Length);
}
}

try
{
System.UInt32 mimeType;
System.UInt32 returnValue = FindMimeFromData(0, null,
buffer, 256, null, 0, out mimeType, 0);
System.IntPtr mimeTypePointer = new IntPtr(mimeType);

return Marshal.PtrToStringUni(mimeTypePointer);
}

catch (Exception ex)
{
return ex.Message;
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top