您的位置:首页 > 编程语言 > C#

C#获取文件属性信息

2013-04-26 21:56 513 查看
方法一:

项目中引用 Shell32.dll;(文件可以从C:\windows\system32\ 目录下拷贝)

using Shell32;

public static string GetFileDetailInfo(string strFilePath, int iIndex)
{
try
{
ShellClass sh = new ShellClass();
Shell32.Folder folder = sh.NameSpace(strFilePath.Substring(0, strFilePath.LastIndexOf("\\")));
Shell32.FolderItem folderItem = folder.ParseName(strFilePath.Substring(strFilePath.LastIndexOf("\\") + 1));
return folder.GetDetailsOf(folderItem, iIndex);
}
catch (Exception ex)
{
ex.Message.ToString();
return null;
}
}






结果:(前面的数字为index,暂时只测了前几个,后面还有很多,可以自己自行测试)

-1: 全部

类型: C Source

作者: peng

标题: 标题1

主题: 主题1

备注1

备注2

修改日期: 2013-4-26 09:43

大小: 2.53 KB

0:文件名

pub_errno2.c

1:文件大小

3 KB

2:文件类型

C Source

3:修改时间

2013-4-26 09:43

4:创建时间

2013-4-26 09:42

5:访问时间

2013-4-26 09:43

6:A

7:在线

8:

DCF39834E33B4B7\work

9:作者

peng

10:标题

标题1

11:主题

主题1

12:类别

类别1

13:

14:备注

备注1

备注2

15:

缺点:以上方式只能获取文件属性,不能设置文件属性;但由于其为系统自带的dll文件,不需要注册。

另外在使用时会提示:无法嵌入互操作类型“……”,需要修改引用的设置:

选中项目中引入的dll,鼠标右键,选择属性,把“嵌入互操作类型”设置为False。

方法二:

使用DSOFile.dll文件(微软发布的,可以自己在网上下载)

使用需要注册

using DSOFile;

try
{
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "regsvr32";
p.StartInfo.Arguments = @"/s E:\test\WpfApplication2\WpfApplication2\DSOFile.dll";
p.Start();
p.WaitForExit();
p.Close();
p.Dispose();
}
catch (System.Exception)
{
MessageBox.Show("注册失败!");
}

DSOFile.OleDocumentProperties dso = new DSOFile.OleDocumentProperties();
dso.Open(strPath, false, DSOFile.dsoFileOpenOptions.dsoOptionOpenReadOnlyIfNoWriteAccess);
dso.SummaryProperties.Title = "标题";
dso.SummaryProperties.Subject = "Subject";
dso.SummaryProperties.Company = "Company";
dso.SummaryProperties.Author = "作者";
dso.SummaryProperties.Comments = "备注";
dso.SummaryProperties.Keywords = "关键字";
dso.Save();
dso.Close(false);

MessageBox.Show(dso.SummaryProperties.Comments);
MessageBox.Show(dso.SummaryProperties.Keywords);


以上方法可以获取文件属性信息,也可以设置文件属性信息。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: