首页  编辑  

文件的MIME类型

Tags: /超级猛料/Stream.File.流、文件和目录/文件操作/   Date Created:

获取文件的MIMEType

笨办法,其实可以使用Windows的注册表。

{$R mimetypes.RES}

function GetMIMEType(FileExt: string): string;

var

 I: Integer;

 S: array[0..255] of Char;

const

 MIMEStart = 101;

 // ID of first MIME Type string (IDs are set in the .rc file

 // before compiling with brcc32)

 MIMEEnd = 742; //ID of last MIME Type string

begin

 Result := 'text/plain';

 // If the file extenstion is not found then the result is plain text

 for I := MIMEStart to MIMEEnd do

 begin

   LoadString(hInstance, I, @S, 255);

   // Loads a string from mimetypes.res which is embedded into the

   // compiled exe

   if Copy(S, 1, Length(FileExt)) = FileExt then

   // "If the string that was loaded contains FileExt then"

   begin

     Result := Copy(S, Length(FileExt) + 2, 255);

     // Copies the MIME Type from the string that was loaded

     Break;

     // Breaks the for loop so that it won't go through every

     // MIME Type after it found the correct one.

   end;

 end;

end;

mimetypes.rc (19.1KB)