会员
周边
新闻
博问
融资
云市场
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
人生之塔
博客园
首页
新随笔
联系
订阅
管理
通过WinForm获取本地文件的ContentType值
Code
1
using
System;
2
using
Microsoft.Win32;
3
4
namespace
FrameworkExamples
5
{
6
//
HOW TO: work with the Windows Registry to determine the
7
//
content-type of a file based on the file extension.
8
9
class
SampleRegistryContentTypeLookup
10
{
11
static
void
Main()
12
{
13
//
set the default content-type
14
const
string
DEFAULT_CONTENT_TYPE
=
"
application/unknown
"
;
15
16
RegistryKey regkey, fileextkey;
17
string
filecontenttype, fileextension;
18
19
//
the file extension to lookup
20
fileextension
=
"
.zip
"
;
21
22
try
23
{
24
//
look in HKCR
25
regkey
=
Registry.ClassesRoot;
26
27
//
look for extension
28
fileextkey
=
regkey.OpenSubKey(fileextension);
29
30
//
retrieve Content Type value
31
filecontenttype
=
fileextkey.GetValue(
"
Content Type
"
, DEFAULT_CONTENT_TYPE).ToString();
32
33
//
cleanup
34
fileextkey
=
null
;
35
regkey
=
null
;
36
}
37
catch
38
{
39
filecontenttype
=
DEFAULT_CONTENT_TYPE;
40
}
41
42
//
print the content type
43
Console.WriteLine(filecontenttype);
44
}
45
}
46
}
47
posted @
2009-07-02 16:25
Rick Sun
阅读(
482
) 评论(
0
)
编辑
收藏
举报
会员力量,点亮园子希望
刷新页面
返回顶部
公告