我希望通过代码项目中的此函数获取mime类型的本地.flv文件 http://www.codeproject.com/KB/dotnet/ContentType.aspx
Public Function GetMIMEType(_
ByVal filepath As String) As String
Dim regPerm As RegistryPermission = _
New RegistryPermission(RegistryPermissionAccess.Read, _
"\\HKEY_CLASSES_ROOT")
Dim classesRoot As RegistryKey = Registry.ClassesRoot
Dim fi = New FileInfo(filepath)
Dim dotExt As String = LCase(fi.Extension)
Dim typeKey As RegistryKey = classesRoot.OpenSubKey(_
"MIME\Database\Content Type")
Dim keyname As String
For Each keyname In typeKey.GetSubKeyNames()
Dim curKey As RegistryKey = classesRoot.OpenSubKey( _
"MIME\Database\Content Type\" & keyname)
If LCase(curKey.GetValue("Extension")) = dotExt Then
'Debug.WriteLine("Content type was " & keyname)
Return keyname
End If
Next
End Function
但它将.flv文件的mime类型返回为x-x509-ca-cert
答案 0 :(得分:4)
您的方法有几个缺点:
尝试使用以下类来获取mime类型。它们将通过混合嗅探文件的前256个字节(urlmon.dll - f.e.由Internet Explorer使用)和一组已知类型来识别。
Imports System.Runtime.InteropServices
Imports System.IO
Public Class GetMimeType
'Test-Function'
Private Sub BtnGetMymeType_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnGetMymeType.Click
Dim fileSelector As New System.Windows.Forms.OpenFileDialog
If fileSelector.ShowDialog = Windows.Forms.DialogResult.OK Then
MessageBox.Show(MimeTypes.GetContentType(fileSelector.FileName))
End If
End Sub
End Class
Public Class MimeTypes
Private Shared knownTypes As List(Of String)
Private Shared mimeTypes As Dictionary(Of String, String)
<DllImport("urlmon.dll", CharSet:=CharSet.Auto)> _
Private Shared Function FindMimeFromData(ByVal pBC As UInt32, <MarshalAs(UnmanagedType.LPStr)> ByVal pwzUrl As String, <MarshalAs(UnmanagedType.LPArray)> ByVal pBuffer As Byte(), ByVal cbSize As UInt32, <MarshalAs(UnmanagedType.LPStr)> ByVal pwzMimeProposed As String, ByVal dwMimeFlags As UInt32, ByRef ppwzMimeOut As UInt32, ByVal dwReserverd As UInt32) As UInt32
End Function
Public Shared Function GetContentType(ByVal fileName As String) As String
If knownTypes Is Nothing OrElse mimeTypes Is Nothing Then InitializeMimeTypeLists()
Dim contentType As String = ""
Dim extension As String = IO.Path.GetExtension(fileName).Replace(".", "").ToLower
mimeTypes.TryGetValue(extension, contentType)
If String.IsNullOrEmpty(contentType) OrElse knownTypes.Contains(contentType) Then
Dim headerType As String = ScanFileForMimeType(fileName)
If headerType <> "application/octet-stream" OrElse String.IsNullOrEmpty(contentType) Then contentType = headerType
End If
Return contentType
End Function
Private Shared Function ScanFileForMimeType(ByVal fileName As String) As String
Try
Dim buffer As Byte() = New Byte(255) {}
Using fs As New FileStream(fileName, FileMode.Open)
Dim readLength As Integer = CInt(Math.Min(256, fs.Length))
fs.Read(buffer, 0, readLength)
End Using
Dim mimeType As UInt32
FindMimeFromData(0, Nothing, buffer, 256, Nothing, 0, mimeType, 0)
Dim mimeTypePtr As IntPtr = New IntPtr(mimeType)
Dim mime As String = Marshal.PtrToStringUni(mimeTypePtr)
Marshal.FreeCoTaskMem(mimeTypePtr)
If String.IsNullOrEmpty(mime) Then mime = "application/octet-stream"
Return mime
Catch ex As Exception
Return "application/octet-stream"
End Try
End Function
Private Shared Sub InitializeMimeTypeLists()
knownTypes = New String() {"text/plain", "text/html", "text/xml", "text/richtext", "text/scriptlet", _
"audio/x-aiff", "audio/basic", "audio/mid", "audio/wav", _
"image/gif", "image/jpeg", "image/pjpeg", "image/png", "image/x-png", "image/tiff", "image/bmp", _
"image/x-xbitmap", "image/x-jg", "image/x-emf", "image/x-wmf", "video/avi", "video/mpeg", _
"application/octet-stream", "application/postscript", "application/base64", "application/macbinhex40", _
"application/pdf", "application/xml", "application/atom+xml", "application/rss+xml", _
"application/x-compressed", "application/x-zip-compressed", "application/x-gzip-compressed", _
"application/java", "application/x-msdownload" _
}.ToList
mimeTypes = New Dictionary(Of String, String)
With mimeTypes
.Add("3dm", "x-world/x-3dmf")
.Add("3dmf", "x-world/x-3dmf")
.Add("a", "application/octet-stream")
.Add("aab", "application/x-authorware-bin")
.Add("aam", "application/x-authorware-map")
.Add("aas", "application/x-authorware-seg")
.Add("abc", "text/vnd.abc")
.Add("acgi", "text/html")
.Add("afl", "video/animaflex")
.Add("ai", "application/postscript")
.Add("aif", "audio/aiff")
.Add("aifc", "audio/aiff")
.Add("aiff", "audio/aiff")
.Add("aim", "application/x-aim")
.Add("aip", "text/x-audiosoft-intra")
.Add("ani", "application/x-navi-animation")
.Add("aos", "application/x-nokia-9000-communicator-add-on-software")
.Add("aps", "application/mime")
.Add("arc", "application/octet-stream")
.Add("arj", "application/arj")
.Add("art", "image/x-jg")
.Add("asf", "video/x-ms-asf")
.Add("asm", "text/x-asm")
.Add("asp", "text/asp")
.Add("asx", "application/x-mplayer2")
.Add("au", "audio/basic")
.Add("avi", "video/avi")
.Add("avs", "video/avs-video")
.Add("bcpio", "application/x-bcpio")
.Add("bin", "application/octet-stream")
.Add("bm", "image/bmp")
.Add("bmp", "image/bmp")
.Add("boo", "application/book")
.Add("book", "application/book")
.Add("boz", "application/x-bzip2")
.Add("bsh", "application/x-bsh")
.Add("bz", "application/x-bzip")
.Add("bz2", "application/x-bzip2")
.Add("c", "text/plain")
.Add("c++", "text/plain")
.Add("cat", "application/vnd.ms-pki.seccat")
.Add("cc", "text/plain")
.Add("ccad", "application/clariscad")
.Add("cco", "application/x-cocoa")
.Add("cdf", "application/cdf")
.Add("cer", "application/pkix-cert")
.Add("cha", "application/x-chat")
.Add("chat", "application/x-chat")
.Add("class", "application/java")
.Add("com", "application/octet-stream")
.Add("conf", "text/plain")
.Add("cpio", "application/x-cpio")
.Add("cpp", "text/x-c")
.Add("cpt", "application/x-cpt")
.Add("crl", "application/pkcs-crl")
.Add("css", "text/css")
.Add("def", "text/plain")
.Add("der", "application/x-x509-ca-cert")
.Add("dif", "video/x-dv")
.Add("dir", "application/x-director")
.Add("dl", "video/dl")
.Add("doc", "application/msword")
.Add("dot", "application/msword")
.Add("dp", "application/commonground")
.Add("drw", "application/drafting")
.Add("dump", "application/octet-stream")
.Add("dv", "video/x-dv")
.Add("dvi", "application/x-dvi")
.Add("dwf", "drawing/x-dwf (old)")
.Add("dwg", "application/acad")
.Add("dxf", "application/dxf")
.Add("eps", "application/postscript")
.Add("es", "application/x-esrehber")
.Add("etx", "text/x-setext")
.Add("evy", "application/envoy")
.Add("exe", "application/octet-stream")
.Add("f", "text/plain")
.Add("f90", "text/x-fortran")
.Add("fdf", "application/vnd.fdf")
.Add("fif", "image/fif")
.Add("fli", "video/fli")
.Add("flv", "video/x-flv")
.Add("for", "text/x-fortran")
.Add("fpx", "image/vnd.fpx")
.Add("g", "text/plain")
.Add("g3", "image/g3fax")
.Add("gif", "image/gif")
.Add("gl", "video/gl")
.Add("gsd", "audio/x-gsm")
.Add("gtar", "application/x-gtar")
.Add("gz", "application/x-compressed")
.Add("h", "text/plain")
.Add("help", "application/x-helpfile")
.Add("hgl", "application/vnd.hp-hpgl")
.Add("hh", "text/plain")
.Add("hlp", "application/x-winhelp")
.Add("htc", "text/x-component")
.Add("htm", "text/html")
.Add("html", "text/html")
.Add("htmls", "text/html")
.Add("htt", "text/webviewhtml")
.Add("htx", "text/html")
.Add("ice", "x-conference/x-cooltalk")
.Add("ico", "image/x-icon")
.Add("idc", "text/plain")
.Add("ief", "image/ief")
.Add("iefs", "image/ief")
.Add("iges", "application/iges")
.Add("igs", "application/iges")
.Add("ima", "application/x-ima")
.Add("imap", "application/x-httpd-imap")
.Add("inf", "application/inf")
.Add("ins", "application/x-internett-signup")
.Add("ip", "application/x-ip2")
.Add("isu", "video/x-isvideo")
.Add("it", "audio/it")
.Add("iv", "application/x-inventor")
.Add("ivr", "i-world/i-vrml")
.Add("ivy", "application/x-livescreen")
.Add("jam", "audio/x-jam")
.Add("jav", "text/plain")
.Add("java", "text/plain")
.Add("jcm", "application/x-java-commerce")
.Add("jfif", "image/jpeg")
.Add("jfif-tbnl", "image/jpeg")
.Add("jpe", "image/jpeg")
.Add("jpeg", "image/jpeg")
.Add("jpg", "image/jpeg")
.Add("jps", "image/x-jps")
.Add("js", "application/x-javascript")
.Add("jut", "image/jutvision")
.Add("kar", "audio/midi")
.Add("ksh", "application/x-ksh")
.Add("la", "audio/nspaudio")
.Add("lam", "audio/x-liveaudio")
.Add("latex", "application/x-latex")
.Add("lha", "application/lha")
.Add("lhx", "application/octet-stream")
.Add("list", "text/plain")
.Add("lma", "audio/nspaudio")
.Add("log", "text/plain")
.Add("lsp", "application/x-lisp")
.Add("lst", "text/plain")
.Add("lsx", "text/x-la-asf")
.Add("ltx", "application/x-latex")
.Add("lzh", "application/octet-stream")
.Add("lzx", "application/lzx")
.Add("m", "text/plain")
.Add("m1v", "video/mpeg")
.Add("m2a", "audio/mpeg")
.Add("m2v", "video/mpeg")
.Add("m3u", "audio/x-mpequrl")
.Add("man", "application/x-troff-man")
.Add("map", "application/x-navimap")
.Add("mar", "text/plain")
.Add("mbd", "application/mbedlet")
.Add("mc$", "application/x-magic-cap-package-1.0")
.Add("mcd", "application/mcad")
.Add("mcf", "image/vasa")
.Add("mcp", "application/netmc")
.Add("me", "application/x-troff-me")
.Add("mht", "message/rfc822")
.Add("mhtml", "message/rfc822")
.Add("mid", "audio/midi")
.Add("midi", "audio/midi")
.Add("mif", "application/x-frame")
.Add("mime", "message/rfc822")
.Add("mjf", "audio/x-vnd.audioexplosion.mjuicemediafile")
.Add("mjpg", "video/x-motion-jpeg")
.Add("mm", "application/base64")
.Add("mme", "application/base64")
.Add("mod", "audio/mod")
.Add("moov", "video/quicktime")
.Add("mov", "video/quicktime")
.Add("movie", "video/x-sgi-movie")
.Add("mp2", "audio/mpeg")
.Add("mp3", "audio/mpeg3")
.Add("mpa", "audio/mpeg")
.Add("mpc", "application/x-project")
.Add("mpe", "video/mpeg")
.Add("mpeg", "video/mpeg")
.Add("mpg", "video/mpeg")
.Add("mpga", "audio/mpeg")
.Add("mpp", "application/vnd.ms-project")
.Add("mpt", "application/x-project")
.Add("mpv", "application/x-project")
.Add("mpx", "application/x-project")
.Add("mrc", "application/marc")
.Add("ms", "application/x-troff-ms")
.Add("mv", "video/x-sgi-movie")
.Add("my", "audio/make")
.Add("mzz", "application/x-vnd.audioexplosion.mzz")
.Add("nap", "image/naplps")
.Add("naplps", "image/naplps")
.Add("nc", "application/x-netcdf")
.Add("ncm", "application/vnd.nokia.configuration-message")
.Add("nif", "image/x-niff")
.Add("niff", "image/x-niff")
.Add("nix", "application/x-mix-transfer")
.Add("nsc", "application/x-conference")
.Add("nvd", "application/x-navidoc")
.Add("o", "application/octet-stream")
.Add("oda", "application/oda")
.Add("omc", "application/x-omc")
.Add("omcd", "application/x-omcdatamaker")
.Add("omcr", "application/x-omcregerator")
.Add("p", "text/x-pascal")
.Add("p10", "application/pkcs10")
.Add("p12", "application/pkcs-12")
.Add("p7a", "application/x-pkcs7-signature")
.Add("p7c", "application/pkcs7-mime")
.Add("pas", "text/pascal")
.Add("pbm", "image/x-portable-bitmap")
.Add("pcl", "application/vnd.hp-pcl")
.Add("pct", "image/x-pict")
.Add("pcx", "image/x-pcx")
.Add("pdf", "application/pdf")
.Add("pfunk", "audio/make")
.Add("pgm", "image/x-portable-graymap")
.Add("pic", "image/pict")
.Add("pict", "image/pict")
.Add("pkg", "application/x-newton-compatible-pkg")
.Add("pko", "application/vnd.ms-pki.pko")
.Add("pl", "text/plain")
.Add("plx", "application/x-pixclscript")
.Add("pm", "image/x-xpixmap")
.Add("png", "image/png")
.Add("pnm", "application/x-portable-anymap")
.Add("pot", "application/mspowerpoint")
.Add("pov", "model/x-pov")
.Add("ppa", "application/vnd.ms-powerpoint")
.Add("ppm", "image/x-portable-pixmap")
.Add("pps", "application/mspowerpoint")
.Add("ppt", "application/mspowerpoint")
.Add("ppz", "application/mspowerpoint")
.Add("pre", "application/x-freelance")
.Add("prt", "application/pro_eng")
.Add("ps", "application/postscript")
.Add("psd", "application/octet-stream")
.Add("pvu", "paleovu/x-pv")
.Add("pwz", "application/vnd.ms-powerpoint")
.Add("py", "text/x-script.phyton")
.Add("pyc", "applicaiton/x-bytecode.python")
.Add("qcp", "audio/vnd.qcelp")
.Add("qd3", "x-world/x-3dmf")
.Add("qd3d", "x-world/x-3dmf")
.Add("qif", "image/x-quicktime")
.Add("qt", "video/quicktime")
.Add("qtc", "video/x-qtc")
.Add("qti", "image/x-quicktime")
.Add("qtif", "image/x-quicktime")
.Add("ra", "audio/x-pn-realaudio")
.Add("ram", "audio/x-pn-realaudio")
.Add("ras", "application/x-cmu-raster")
.Add("rast", "image/cmu-raster")
.Add("rexx", "text/x-script.rexx")
.Add("rf", "image/vnd.rn-realflash")
.Add("rgb", "image/x-rgb")
.Add("rm", "application/vnd.rn-realmedia")
.Add("rmi", "audio/mid")
.Add("rmm", "audio/x-pn-realaudio")
.Add("rmp", "audio/x-pn-realaudio")
.Add("rng", "application/ringing-tones")
.Add("rnx", "application/vnd.rn-realplayer")
.Add("roff", "application/x-troff")
.Add("rp", "image/vnd.rn-realpix")
.Add("rpm", "audio/x-pn-realaudio-plugin")
.Add("rt", "text/richtext")
.Add("rtf", "text/richtext")
.Add("rtx", "application/rtf")
.Add("rv", "video/vnd.rn-realvideo")
.Add("s", "text/x-asm")
.Add("s3m", "audio/s3m")
.Add("saveme", "application/octet-stream")
.Add("sbk", "application/x-tbook")
.Add("scm", "application/x-lotusscreencam")
.Add("sdml", "text/plain")
.Add("sdp", "application/sdp")
.Add("sdr", "application/sounder")
.Add("sea", "application/sea")
.Add("set", "application/set")
.Add("sgm", "text/sgml")
.Add("sgml", "text/sgml")
.Add("sh", "application/x-bsh")
.Add("shtml", "text/html")
.Add("sid", "audio/x-psid")
.Add("sit", "application/x-sit")
.Add("skd", "application/x-koan")
.Add("skm", "application/x-koan")
.Add("skp", "application/x-koan")
.Add("skt", "application/x-koan")
.Add("sl", "application/x-seelogo")
.Add("smi", "application/smil")
.Add("smil", "application/smil")
.Add("snd", "audio/basic")
.Add("sol", "application/solids")
.Add("spc", "application/x-pkcs7-certificates")
.Add("spl", "application/futuresplash")
.Add("spr", "application/x-sprite")
.Add("sprite", "application/x-sprite")
.Add("src", "application/x-wais-source")
.Add("ssi", "text/x-server-parsed-html")
.Add("ssm", "application/streamingmedia")
.Add("sst", "application/vnd.ms-pki.certstore")
.Add("step", "application/step")
.Add("stl", "application/sla")
.Add("stp", "application/step")
.Add("sv4cpio", "application/x-sv4cpio")
.Add("sv4crc", "application/x-sv4crc")
.Add("svf", "image/vnd.dwg")
.Add("svr", "application/x-world")
.Add("swf", "application/x-shockwave-flash")
.Add("t", "application/x-troff")
.Add("talk", "text/x-speech")
.Add("tar", "application/x-tar")
.Add("tbk", "application/toolbook")
.Add("tcl", "application/x-tcl")
.Add("tcsh", "text/x-script.tcsh")
.Add("tex", "application/x-tex")
.Add("texi", "application/x-texinfo")
.Add("texinfo", "application/x-texinfo")
.Add("text", "text/plain")
.Add("tgz", "application/x-compressed")
.Add("tif", "image/tiff")
.Add("tr", "application/x-troff")
.Add("tsi", "audio/tsp-audio")
.Add("tsp", "audio/tsplayer")
.Add("tsv", "text/tab-separated-values")
.Add("turbot", "image/florian")
.Add("txt", "text/plain")
.Add("uil", "text/x-uil")
.Add("uni", "text/uri-list")
.Add("unis", "text/uri-list")
.Add("unv", "application/i-deas")
.Add("uri", "text/uri-list")
.Add("uris", "text/uri-list")
.Add("ustar", "application/x-ustar")
.Add("uu", "application/octet-stream")
.Add("vcd", "application/x-cdlink")
.Add("vcs", "text/x-vcalendar")
.Add("vda", "application/vda")
.Add("vdo", "video/vdo")
.Add("vew", "application/groupwise")
.Add("viv", "video/vivo")
.Add("vivo", "video/vivo")
.Add("vmd", "application/vocaltec-media-desc")
.Add("vmf", "application/vocaltec-media-file")
.Add("voc", "audio/voc")
.Add("vos", "video/vosaic")
.Add("vox", "audio/voxware")
.Add("vqe", "audio/x-twinvq-plugin")
.Add("vqf", "audio/x-twinvq")
.Add("vql", "audio/x-twinvq-plugin")
.Add("vrml", "application/x-vrml")
.Add("vrt", "x-world/x-vrt")
.Add("vsd", "application/x-visio")
.Add("vst", "application/x-visio")
.Add("vsw", "application/x-visio")
.Add("w60", "application/wordperfect6.0")
.Add("w61", "application/wordperfect6.1")
.Add("w6w", "application/msword")
.Add("wav", "audio/wav")
.Add("wb1", "application/x-qpro")
.Add("wbmp", "image/vnd.wap.wbmp")
.Add("web", "application/vnd.xara")
.Add("wiz", "application/msword")
.Add("wk1", "application/x-123")
.Add("wmf", "windows/metafile")
.Add("wml", "text/vnd.wap.wml")
.Add("wmlc", "application/vnd.wap.wmlc")
.Add("wmls", "text/vnd.wap.wmlscript")
.Add("wmlsc", "application/vnd.wap.wmlscriptc")
.Add("word", "application/msword")
.Add("wp", "application/wordperfect")
.Add("wp5", "application/wordperfect")
.Add("wp6", "application/wordperfect")
.Add("wpd", "application/wordperfect")
.Add("wq1", "application/x-lotus")
.Add("wri", "application/mswrite")
.Add("wrl", "application/x-world")
.Add("wrz", "model/vrml")
.Add("wsc", "text/scriplet")
.Add("wsrc", "application/x-wais-source")
.Add("wtk", "application/x-wintalk")
.Add("xbm", "image/x-xbitmap")
.Add("xdr", "video/x-amt-demorun")
.Add("xgz", "xgl/drawing")
.Add("xif", "image/vnd.xiff")
.Add("xl", "application/excel")
.Add("xla", "application/excel")
.Add("xlb", "application/excel")
.Add("xlc", "application/excel")
.Add("xld", "application/excel")
.Add("xlk", "application/excel")
.Add("xll", "application/excel")
.Add("xlm", "application/excel")
.Add("xls", "application/excel")
.Add("xlt", "application/excel")
.Add("xlv", "application/excel")
.Add("xlw", "application/excel")
.Add("xm", "audio/xm")
.Add("xml", "text/xml")
.Add("xmz", "xgl/movie")
.Add("xpix", "application/x-vnd.ls-xpix")
.Add("xpm", "image/x-xpixmap")
.Add("x-png", "image/png")
.Add("xsr", "video/x-amt-showrun")
.Add("xwd", "image/x-xwd")
.Add("xyz", "chemical/x-pdb")
.Add("z", "application/x-compress")
.Add("zip", "application/x-compressed")
.Add("zoo", "application/octet-stream")
.Add("zsh", "text/x-script.zsh")
End With
End Sub
End Class
当mime类型未知时,您必须更新此列表,并且不足以知道它是application / octet-stream。检查扩展名是否为txt但mime-type是二进制数据可能就足够了,因为这意味着该文件不是它假装的文件。