加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – Urlmon.dll中具有更多MIME类型的FindMimeFromData方法的替

发布时间:2020-12-15 17:48:22 所属栏目:百科 来源:网络整理
导读:通过 Windows DLL Urlmon.dll可访问的 FindMimeFromData方法能够确定存储在内存中的给定数据的MIME类型,考虑到存储此类数据的字节数组的前256个字节. 但是在阅读了它的文档之后,我在MIME Type Detection in Windows Internet Explorer中找到了这个方法能够识
通过 Windows DLL Urlmon.dll可访问的 FindMimeFromData方法能够确定存储在内存中的给定数据的MIME类型,考虑到存储此类数据的字节数组的前256个字节.

但是在阅读了它的文档之后,我在MIME Type Detection in Windows Internet Explorer中找到了这个方法能够识别的MIME类型.请参阅list.如您所见,此方法仅限于26种MIME类型.

所以我想知道是否有人能指出我有更多MIME类型的另一种方法,或者另外一种方法/类我可以包含我认为合适的MIME类型.

谢谢.

解决方法

So I was wondering if anyone could point me to another method with
more MIME types,or alternatively another method / class were I would
be able to include the MIME types I see fit.

我使用Winista和URLMon的混合来检测上传文件的真实格式.

下载Winista:http://www.netomatix.com/Products/DocumentManagement/MimeDetector.aspx

或者使用URLMon下载项目:
https://github.com/MeaningOfLights/MimeDetect

Winista MIME检测

假设有人使用jpg扩展名重命名exe,您仍然可以使用二进制分析确定“真实”文件格式.它不会检测swf或flv,但几乎所有其他众所周知的格式都可以获得十六进制编辑器并添加它可以检测到的更多文件.

文件魔术

Winista使用XML文件“mime-type.xml”检测真实的MIME类型,该文件包含有关文件类型和用于标识内容类型的签名的信息.eg:

<!--
 !   Audio primary type
 ! -->

<mime-type name="audio/basic"
           description="uLaw/AU Audio File">
    <ext>au</ext><ext>snd</ext>
    <magic offset="0" type="byte" value="2e736e64000000"/>
</mime-type>

<mime-type name="audio/midi"
           description="Musical Instrument Digital Interface MIDI-sequention Sound">
    <ext>mid</ext><ext>midi</ext><ext>kar</ext>
    <magic offset="0" value="MThd"/>
</mime-type>

<mime-type name="audio/mpeg"
           description="MPEG Audio Stream,Layer III">
    <ext>mp3</ext><ext>mp2</ext><ext>mpga</ext>
    <magic offset="0" value="ID3"/>
</mime-type>

当Winista无法检测到真正的文件格式时,我已经使用了URLMon方法:

public class urlmonMimeDetect
{
    [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 dwReserverd
    );

public string GetMimeFromFile(string filename)
{
    if (!File.Exists(filename))
        throw new FileNotFoundException(filename + " not found");

    byte[] buffer = new byte[256];
    using (FileStream fs = new FileStream(filename,FileMode.Open,FileAccess.Read))
    {
        if (fs.Length >= 256)
            fs.Read(buffer,256);
        else
            fs.Read(buffer,(int)fs.Length);
    }
    try
    {
        System.UInt32 mimetype;
        FindMimeFromData(0,null,buffer,256,out mimetype,0);
        System.IntPtr mimeTypePtr = new IntPtr(mimetype);
        string mime = Marshal.PtrToStringUni(mimeTypePtr);
        Marshal.FreeCoTaskMem(mimeTypePtr);
        return mime;
    }
    catch (Exception e)
    {
        return "unknown/unknown";
    }
}
}

从Winista方法内部,我回到URLMon:

public MimeType GetMimeTypeFromFile(string filePath)
    {
        sbyte[] fileData = null;
        using (FileStream srcFile = new FileStream(filePath,FileAccess.Read))
        {
            byte[] data = new byte[srcFile.Length];
            srcFile.Read(data,(Int32)srcFile.Length);
            fileData = Winista.Mime.SupportUtil.ToSByteArray(data);
        }

        MimeType oMimeType = GetMimeType(fileData);
        if (oMimeType != null) return oMimeType;

        //We haven't found the file using Magic (eg a text/plain file)
        //so instead use URLMon to try and get the files format
        Winista.MimeDetect.URLMONMimeDetect.urlmonMimeDetect urlmonMimeDetect = new Winista.MimeDetect.URLMONMimeDetect.urlmonMimeDetect();
        string urlmonMimeType = urlmonMimeDetect.GetMimeFromFile(filePath);
        if (!string.IsNullOrEmpty(urlmonMimeType))
        {
            foreach (MimeType mimeType in types)
            {
                if (mimeType.Name == urlmonMimeType)
                {
                    return mimeType;
                }
            }
        }

        return oMimeType;
    }

Winista from netomatix.AFAIK是一个基于2000年初开源Java项目的C#重写.请享用!

您也可以使用由Paul Zahra链接的this post中提到的注册表方法或.Net 4.5 method,但Winista是最好的恕我直言.

更新:

对于桌面应用程序,您可能会发现WindowsAPICodePack更好用:

using Microsoft.WindowsAPICodePack.Shell;
using Microsoft.WindowsAPICodePack.Shell.PropertySystem;

private static string GetFilePropertyItemTypeTextValueFromShellFile(string filePathWithExtension)
{
   var shellFile = ShellFile.FromFilePath(filePathWithExtension);
   var prop = shellFile.Properties.GetProperty(PItemTypeTextCanonical);
   return prop.FormatForDisplay(PropertyDescriptionFormatOptions.None);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读