Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / May 2008

Tip: Looking for answers? Try searching our database.

Mime Type of a file

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
tinman77 - 09 May 2008 02:57 GMT
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.
tinman77 - 11 May 2008 17:41 GMT
> Thanks in advance for everyone's help...
>
[quoted text clipped - 25 lines]
>
> 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;
       }
   }
}

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.