获取文件的MIME类型有很多方法,闲话少絮,直接进入主题,下面是使用C#获取文件的MIME类型(Content Type)的各种方法。
第一种:这种获取MIME类型(Content Type)的方法需要在.NET 4.5之后才能够支持,但是非常简单。
MIME TYPE:
1
2
3
4
5
6
7
8
9
10
|
// 需要先行引入System.Web
// 之后可以直接使用MimeMapping类的静态方法获取文件的MIME类型
string
file =
@"mimetype.txt"
;
string
contentType = MimeMapping.GetMimeMapping(file);
Console.WriteLine(
"{0}'s MIME TYPE:{1}"
, file, contentType);
// Console output:
/*
mimetype.txt's Mime Type: text/plain
*/
|
优点:方便快捷
缺点:只能在.NET 4.5之后使用
如果没有的MIME TYPE将会返回:
application/octet-stream
第二种:直接在系统中通过文件的后缀获取文件的Content Type
1
2
3
4
5
6
7
8
9
10
11
12
|
// 自定义一个方法
// 通过系统中的Content Type来获取MIME TYPE
public
static
string
GetMimeType(
string
fileName)
{
string
mimeType =
"application/unknown"
;
string
ext = Path.GetExtension(fileName).ToLower();
RegistryKey regKey = Registry.ClassesRoot.OpenSubKey(ext);
if
(regKey !=
null
&& regKey.GetValue(
"Content Type"
) !=
null
){
mimeType = regKey.GetValue(
"Content Type"
).ToString();
}
return
mimeType;
}
|
优点:
1. 直接快速
2. 没有.NET的版本限制;
缺点:
1. 需要有访问注册表的权限;
2. 获取的Content Type范围受系统环境约束。
这个方法在没有找到对应的MIME Type时,返回的是application/unknow,当然,你也可以自己定义其他任何返回值。
第三种: 自己动手写代码,将MimeType写成Mapping类,需要的时候直接使用。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
// 通过自己定义一个静态类
// 将所有的Content Type都扔进去吧
// 调用的时候直接调用静态方法即可。
public
static
class
MimeMapping
{
private
static
Hashtable _mimeMappingTable;
private
static
void
AddMimeMapping(
string
extension,
string
MimeType)
{
MimeMapping._mimeMappingTable.Add(extension, MimeType);
}
public
static
string
GetMimeMapping(
string
FileName)
{
string
text =
null
;
int
num = FileName.LastIndexOf(
'.'
);
if
(0 < num && num > FileName.LastIndexOf(
'\\'
))
{
text = (
string
)MimeMapping._mimeMappingTable[FileName.Substring(num)];
}
if
(text ==
null
)
{
text = (
string
)MimeMapping._mimeMappingTable[
".*"
];
}
return
text;
}
static
MimeMapping()
{
MimeMapping._mimeMappingTable =
new
Hashtable(190, StringComparer.CurrentCultureIgnoreCase);
MimeMapping.AddMimeMapping(
".323"
,
"text/h323"
);
MimeMapping.AddMimeMapping(
".asx"
,
"video/x-ms-asf"
);
MimeMapping.AddMimeMapping(
".acx"
,
"application/internet-property-stream"
);
MimeMapping.AddMimeMapping(
".ai"
,
"application/postscript"
);
MimeMapping.AddMimeMapping(
".aif"
,
"audio/x-aiff"
);
MimeMapping.AddMimeMapping(
".aiff"
,
"audio/aiff"
);
MimeMapping.AddMimeMapping(
".axs"
,
"application/olescript"
);
MimeMapping.AddMimeMapping(
".aifc"
,
"audio/aiff"
);
MimeMapping.AddMimeMapping(
".asr"
,
"video/x-ms-asf"
);
MimeMapping.AddMimeMapping(
".avi"
,
"video/x-msvideo"
);
MimeMapping.AddMimeMapping(
".asf"
,
"video/x-ms-asf"
);
MimeMapping.AddMimeMapping(
".au"
,
"audio/basic"
);
MimeMapping.AddMimeMapping(
".application"
,
"application/x-ms-application"
);
MimeMapping.AddMimeMapping(
".bin"
,
"application/octet-stream"
);
MimeMapping.AddMimeMapping(
".bas"
,
"text/plain"
);
MimeMapping.AddMimeMapping(
".bcpio"
,
"application/x-bcpio"
);
MimeMapping.AddMimeMapping(
".bmp"
,
"image/bmp"
);
MimeMapping.AddMimeMapping(
".cdf"
,
"application/x-cdf"
);
MimeMapping.AddMimeMapping(
".cat"
,
"application/vndms-pkiseccat"
);
MimeMapping.AddMimeMapping(
".crt"
,
"application/x-x509-ca-cert"
);
MimeMapping.AddMimeMapping(
".c"
,
"text/plain"
);
MimeMapping.AddMimeMapping(
".css"
,
"text/css"
);
MimeMapping.AddMimeMapping(
".cer"
,
"application/x-x509-ca-cert"
);
MimeMapping.AddMimeMapping(
".crl"
,
"application/pkix-crl"
);
MimeMapping.AddMimeMapping(
".cmx"
,
"image/x-cmx"
);
MimeMapping.AddMimeMapping(
".csh"
,
"application/x-csh"
);
MimeMapping.AddMimeMapping(
".cod"
,
"image/cis-cod"
);
MimeMapping.AddMimeMapping(
".cpio"
,
"application/x-cpio"
);
MimeMapping.AddMimeMapping(
".clp"
,
"application/x-msclip"
);
MimeMapping.AddMimeMapping(
".crd"
,
"application/x-mscardfile"
);
MimeMapping.AddMimeMapping(
".deploy"
,
"application/octet-stream"
);
MimeMapping.AddMimeMapping(
".dll"
,
"application/x-msdownload"
);
MimeMapping.AddMimeMapping(
".dot"
,
"application/msword"
);
MimeMapping.AddMimeMapping(
".doc"
,
"application/msword"
);
MimeMapping.AddMimeMapping(
".dvi"
,
"application/x-dvi"
);
MimeMapping.AddMimeMapping(
".dir"
,
"application/x-director"
);
MimeMapping.AddMimeMapping(
".dxr"
,
"application/x-director"
);
MimeMapping.AddMimeMapping(
".der"
,
"application/x-x509-ca-cert"
);
MimeMapping.AddMimeMapping(
".dib"
,
"image/bmp"
);
MimeMapping.AddMimeMapping(
".dcr"
,
"application/x-director"
);
MimeMapping.AddMimeMapping(
".disco"
,
"text/xml"
);
MimeMapping.AddMimeMapping(
".exe"
,
"application/octet-stream"
);
MimeMapping.AddMimeMapping(
".etx"
,
"text/x-setext"
);
MimeMapping.AddMimeMapping(
".evy"
,
"application/envoy"
);
MimeMapping.AddMimeMapping(
".eml"
,
"message/rfc822"
);
MimeMapping.AddMimeMapping(
".eps"
,
"application/postscript"
);
MimeMapping.AddMimeMapping(
".flr"
,
"x-world/x-vrml"
);
MimeMapping.AddMimeMapping(
".fif"
,
"application/fractals"
);
MimeMapping.AddMimeMapping(
".gtar"
,
"application/x-gtar"
);
MimeMapping.AddMimeMapping(
".gif"
,
"image/gif"
);
MimeMapping.AddMimeMapping(
".gz"
,
"application/x-gzip"
);
MimeMapping.AddMimeMapping(
".hta"
,
"application/hta"
);
MimeMapping.AddMimeMapping(
".htc"
,
"text/x-component"
);
MimeMapping.AddMimeMapping(
".htt"
,
"text/webviewhtml"
);
MimeMapping.AddMimeMapping(
".h"
,
"text/plain"
);
MimeMapping.AddMimeMapping(
".hdf"
,
"application/x-hdf"
);
MimeMapping.AddMimeMapping(
".hlp"
,
"application/winhlp"
);
MimeMapping.AddMimeMapping(
".html"
,
"text/html"
);
MimeMapping.AddMimeMapping(
".htm"
,
"text/html"
);
MimeMapping.AddMimeMapping(
".hqx"
,
"application/mac-binhex40"
);
MimeMapping.AddMimeMapping(
".isp"
,
"application/x-internet-signup"
);
MimeMapping.AddMimeMapping(
".iii"
,
"application/x-iphone"
);
MimeMapping.AddMimeMapping(
".ief"
,
"image/ief"
);
MimeMapping.AddMimeMapping(
".ivf"
,
"video/x-ivf"
);
MimeMapping.AddMimeMapping(
".ins"
,
"application/x-internet-signup"
);
MimeMapping.AddMimeMapping(
".ico"
,
"image/x-icon"
);
MimeMapping.AddMimeMapping(
".jpg"
,
"image/jpeg"
);
MimeMapping.AddMimeMapping(
".jfif"
,
"image/pjpeg"
);
MimeMapping.AddMimeMapping(
".jpe"
,
"image/jpeg"
);
MimeMapping.AddMimeMapping(
".jpeg"
,
"image/jpeg"
);
MimeMapping.AddMimeMapping(
".js"
,
"application/x-javascript"
);
MimeMapping.AddMimeMapping(
".lsx"
,
"video/x-la-asf"
);
MimeMapping.AddMimeMapping(
".latex"
,
"application/x-latex"
);
MimeMapping.AddMimeMapping(
".lsf"
,
"video/x-la-asf"
);
MimeMapping.AddMimeMapping(
".manifest"
,
"application/x-ms-manifest"
);
MimeMapping.AddMimeMapping(
".mhtml"
,
"message/rfc822"
);
MimeMapping.AddMimeMapping(
".mny"
,
"application/x-msmoney"
);
MimeMapping.AddMimeMapping(
".mht"
,
"message/rfc822"
);
MimeMapping.AddMimeMapping(
".mid"
,
"audio/mid"
);
MimeMapping.AddMimeMapping(
".mpv2"
,
"video/mpeg"
);
MimeMapping.AddMimeMapping(
".man"
,
"application/x-troff-man"
);
MimeMapping.AddMimeMapping(
".mvb"
,
"application/x-msmediaview"
);
MimeMapping.AddMimeMapping(
".mpeg"
,
"video/mpeg"
);
MimeMapping.AddMimeMapping(
".m3u"
,
"audio/x-mpegurl"
);
MimeMapping.AddMimeMapping(
".mdb"
,
"application/x-msaccess"
);
MimeMapping.AddMimeMapping(
".mpp"
,
"application/vnd.ms-project"
);
MimeMapping.AddMimeMapping(
".m1v"
,
"video/mpeg"
);
MimeMapping.AddMimeMapping(
".mpa"
,
"video/mpeg"
);
MimeMapping.AddMimeMapping(
".me"
,
"application/x-troff-me"
);
MimeMapping.AddMimeMapping(
".m13"
,
"application/x-msmediaview"
);
MimeMapping.AddMimeMapping(
".movie"
,
"video/x-sgi-movie"
);
MimeMapping.AddMimeMapping(
".m14"
,
"application/x-msmediaview"
);
MimeMapping.AddMimeMapping(
".mpe"
,
"video/mpeg"
);
MimeMapping.AddMimeMapping(
".mp2"
,
"video/mpeg"
);
MimeMapping.AddMimeMapping(
".mov"
,
"video/quicktime"
);
MimeMapping.AddMimeMapping(
".mp3"
,
"audio/mpeg"
);
MimeMapping.AddMimeMapping(
".mpg"
,
"video/mpeg"
);
MimeMapping.AddMimeMapping(
".ms"
,
"application/x-troff-ms"
);
MimeMapping.AddMimeMapping(
".nc"
,
"application/x-netcdf"
);
MimeMapping.AddMimeMapping(
".nws"
,
"message/rfc822"
);
MimeMapping.AddMimeMapping(
".oda"
,
"application/oda"
);
MimeMapping.AddMimeMapping(
".ods"
,
"application/oleobject"
);
MimeMapping.AddMimeMapping(
".pmc"
,
"application/x-perfmon"
);
MimeMapping.AddMimeMapping(
".p7r"
,
"application/x-pkcs7-certreqresp"
);
MimeMapping.AddMimeMapping(
".p7b"
,
"application/x-pkcs7-certificates"
);
MimeMapping.AddMimeMapping(
".p7s"
,
"application/pkcs7-signature"
);
MimeMapping.AddMimeMapping(
".pmw"
,
"application/x-perfmon"
);
MimeMapping.AddMimeMapping(
".ps"
,
"application/postscript"
);
MimeMapping.AddMimeMapping(
".p7c"
,
"application/pkcs7-mime"
);
MimeMapping.AddMimeMapping(
".pbm"
,
"image/x-portable-bitmap"
);
MimeMapping.AddMimeMapping(
".ppm"
,
"image/x-portable-pixmap"
);
MimeMapping.AddMimeMapping(
".pub"
,
"application/x-mspublisher"
);
MimeMapping.AddMimeMapping(
".pnm"
,
"image/x-portable-anymap"
);
MimeMapping.AddMimeMapping(
".png"
,
"image/png"
);
MimeMapping.AddMimeMapping(
".pml"
,
"application/x-perfmon"
);
MimeMapping.AddMimeMapping(
".p10"
,
"application/pkcs10"
);
MimeMapping.AddMimeMapping(
".pfx"
,
"application/x-pkcs12"
);
MimeMapping.AddMimeMapping(
".p12"
,
"application/x-pkcs12"
);
MimeMapping.AddMimeMapping(
".pdf"
,
"application/pdf"
);
MimeMapping.AddMimeMapping(
".pps"
,
"application/vnd.ms-powerpoint"
);
MimeMapping.AddMimeMapping(
".p7m"
,
"application/pkcs7-mime"
);
MimeMapping.AddMimeMapping(
".pko"
,
"application/vndms-pkipko"
);
MimeMapping.AddMimeMapping(
".ppt"
,
"application/vnd.ms-powerpoint"
);
MimeMapping.AddMimeMapping(
".pmr"
,
"application/x-perfmon"
);
MimeMapping.AddMimeMapping(
".pma"
,
"application/x-perfmon"
);
MimeMapping.AddMimeMapping(
".pot"
,
"application/vnd.ms-powerpoint"
);
MimeMapping.AddMimeMapping(
".prf"
,
"application/pics-rules"
);
MimeMapping.AddMimeMapping(
".pgm"
,
"image/x-portable-graymap"
);
MimeMapping.AddMimeMapping(
".qt"
,
"video/quicktime"
);
MimeMapping.AddMimeMapping(
".ra"
,
"audio/x-pn-realaudio"
);
MimeMapping.AddMimeMapping(
".rgb"
,
"image/x-rgb"
);
MimeMapping.AddMimeMapping(
".ram"
,
"audio/x-pn-realaudio"
);
MimeMapping.AddMimeMapping(
".rmi"
,
"audio/mid"
);
MimeMapping.AddMimeMapping(
".ras"
,
"image/x-cmu-raster"
);
MimeMapping.AddMimeMapping(
".roff"
,
"application/x-troff"
);
MimeMapping.AddMimeMapping(
".rtf"
,
"application/rtf"
);
MimeMapping.AddMimeMapping(
".rtx"
,
"text/richtext"
);
MimeMapping.AddMimeMapping(
".sv4crc"
,
"application/x-sv4crc"
);
MimeMapping.AddMimeMapping(
".spc"
,
"application/x-pkcs7-certificates"
);
MimeMapping.AddMimeMapping(
".setreg"
,
"application/set-registration-initiation"
);
MimeMapping.AddMimeMapping(
".snd"
,
"audio/basic"
);
MimeMapping.AddMimeMapping(
".stl"
,
"application/vndms-pkistl"
);
MimeMapping.AddMimeMapping(
".setpay"
,
"application/set-payment-initiation"
);
MimeMapping.AddMimeMapping(
".stm"
,
"text/html"
);
MimeMapping.AddMimeMapping(
".shar"
,
"application/x-shar"
);
MimeMapping.AddMimeMapping(
".sh"
,
"application/x-sh"
);
MimeMapping.AddMimeMapping(
".sit"
,
"application/x-stuffit"
);
MimeMapping.AddMimeMapping(
".spl"
,
"application/futuresplash"
);
MimeMapping.AddMimeMapping(
".sct"
,
"text/scriptlet"
);
MimeMapping.AddMimeMapping(
".scd"
,
"application/x-msschedule"
);
MimeMapping.AddMimeMapping(
".sst"
,
"application/vndms-pkicertstore"
);
MimeMapping.AddMimeMapping(
".src"
,
"application/x-wais-source"
);
MimeMapping.AddMimeMapping(
".sv4cpio"
,
"application/x-sv4cpio"
);
MimeMapping.AddMimeMapping(
".tex"
,
"application/x-tex"
);
MimeMapping.AddMimeMapping(
".tgz"
,
"application/x-compressed"
);
MimeMapping.AddMimeMapping(
".t"
,
"application/x-troff"
);
MimeMapping.AddMimeMapping(
".tar"
,
"application/x-tar"
);
MimeMapping.AddMimeMapping(
".tr"
,
"application/x-troff"
);
MimeMapping.AddMimeMapping(
".tif"
,
"image/tiff"
);
MimeMapping.AddMimeMapping(
".txt"
,
"text/plain"
);
MimeMapping.AddMimeMapping(
".texinfo"
,
"application/x-texinfo"
);
MimeMapping.AddMimeMapping(
".trm"
,
"application/x-msterminal"
);
MimeMapping.AddMimeMapping(
".tiff"
,
"image/tiff"
);
MimeMapping.AddMimeMapping(
".tcl"
,
"application/x-tcl"
);
MimeMapping.AddMimeMapping(
".texi"
,
"application/x-texinfo"
);
MimeMapping.AddMimeMapping(
".tsv"
,
"text/tab-separated-values"
);
MimeMapping.AddMimeMapping(
".ustar"
,
"application/x-ustar"
);
MimeMapping.AddMimeMapping(
".uls"
,
"text/iuls"
);
MimeMapping.AddMimeMapping(
".vcf"
,
"text/x-vcard"
);
MimeMapping.AddMimeMapping(
".wps"
,
"application/vnd.ms-works"
);
MimeMapping.AddMimeMapping(
".wav"
,
"audio/wav"
);
MimeMapping.AddMimeMapping(
".wrz"
,
"x-world/x-vrml"
);
MimeMapping.AddMimeMapping(
".wri"
,
"application/x-mswrite"
);
MimeMapping.AddMimeMapping(
".wks"
,
"application/vnd.ms-works"
);
MimeMapping.AddMimeMapping(
".wmf"
,
"application/x-msmetafile"
);
MimeMapping.AddMimeMapping(
".wcm"
,
"application/vnd.ms-works"
);
MimeMapping.AddMimeMapping(
".wrl"
,
"x-world/x-vrml"
);
MimeMapping.AddMimeMapping(
".wdb"
,
"application/vnd.ms-works"
);
MimeMapping.AddMimeMapping(
".wsdl"
,
"text/xml"
);
MimeMapping.AddMimeMapping(
".xap"
,
"application/x-silverlight-app"
);
MimeMapping.AddMimeMapping(
".xml"
,
"text/xml"
);
MimeMapping.AddMimeMapping(
".xlm"
,
"application/vnd.ms-excel"
);
MimeMapping.AddMimeMapping(
".xaf"
,
"x-world/x-vrml"
);
MimeMapping.AddMimeMapping(
".xla"
,
"application/vnd.ms-excel"
);
MimeMapping.AddMimeMapping(
".xls"
,
"application/vnd.ms-excel"
);
MimeMapping.AddMimeMapping(
".xof"
,
"x-world/x-vrml"
);
MimeMapping.AddMimeMapping(
".xlt"
,
"application/vnd.ms-excel"
);
MimeMapping.AddMimeMapping(
".xlc"
,
"application/vnd.ms-excel"
);
MimeMapping.AddMimeMapping(
".xsl"
,
"text/xml"
);
MimeMapping.AddMimeMapping(
".xbm"
,
"image/x-xbitmap"
);
MimeMapping.AddMimeMapping(
".xlw"
,
"application/vnd.ms-excel"
);
MimeMapping.AddMimeMapping(
".xpm"
,
"image/x-xpixmap"
);
MimeMapping.AddMimeMapping(
".xwd"
,
"image/x-xwindowdump"
);
MimeMapping.AddMimeMapping(
".xsd"
,
"text/xml"
);
MimeMapping.AddMimeMapping(
".z"
,
"application/x-compress"
);
MimeMapping.AddMimeMapping(
".zip"
,
"application/x-zip-compressed"
);
MimeMapping.AddMimeMapping(
".*"
,
"application/octet-stream"
);
}
}
//需要调用的时候
string
file =
@"MimeType.txt"
;
string
contentType = MimeMapping.GetMimeMapping(file);
Console。WriteLine(
"{0}'s MIME TYPE: {1}"
, file, contentType);
// ~~~~
|