在C#中获得文件信息很容易,只需要用FileInfo类或者FileVersionInfo类就可以获得,但是如果想要获得文件的扩展信息,则无法从这两类来获得。不过在C#中,这也不是件难事,只要引入“Microsoft Shell Controls and Automation”这个COM就可以获得。

接下来就分别来介绍。

首先介绍FileInfo类,这个类非常简单,首先需要根据文件名来创建FileInfo对象,例如:

using System.IO;

FileInfo fi = new FileInfo( yourFileName );

那么以后就可以通过此对象来访问文件一些属性,例如文件大小,创建时间,最后访问时间,最后写入时间等等,还可以通过访问对象的Attributes属性,来获得当前文件是只读、隐藏之类属性,这里我就不细说了,详情参看MSDN。

第二个要说的,就是FileSystemInfo类,这个类是FileInfo类的基类,这里也就不多说了。

第三个要说的,就是如何判断一个文件的Version信息,这就需要来介绍FileVersionInfo这个类。但是并不是所有的文件都有Version信息,因此在使用FileVersionInfo的时候需要注意的是,最好先判断一下文件的扩展名。不过一个FileVersionInfo类对象不能通过构造函数来创建,需要调用类的静态方法来获得,例如:

using System.Diagnostics;

FileVersionInfo fvi = FileVersionInfo.GetVersionInfo( yourFileName );

通过此对象,可以获得文件的产品名称,公司名,版本号,语言版本,版权等等,这方面详情可以参看MSDN。

最后要说的,就是如何得到一个文件的扩展信息,例如标题,作者等等,这些信息从如上三个类中是无法获得。但是要在C#程序中获得,就需要引入一个“Microsoft Shell Controls and Automation”这个COM,这个COM是由系统“Shell32.dll”而提供。这方面的例子,可以参看如下这篇文章。

http://www.codeproject.com/cs/files/detailedfileinfo.asp

为了方便大家使用,我把其中类的代码贴出来。

using Shell32; // Use this namespace after add the reference

/// <summary>

/// Returns the detailed Information of a given file.

/// </summary>

public class CFileInfo

{

private string sFileName ="",

sFullFileName="",

sFileExtension="",

sFilePath = "",

sFileComment = "",

sFileAuthor = "",

sFileTitle = "",

sFileSubject = "",

sFileCategory = "",

sFileType = "";

private long lFileLength = 0,

lFileVersion = 0;

private DateTime dCreationDate,

dModificationDate;

public CFileInfo(string sFPath)

{

// check if the given File exists

if(File.Exists(sFPath))

{

ArrayList aDetailedInfo = new ArrayList();

FileInfo oFInfo = new FileInfo(sFPath);

sFileName = oFInfo.Name;

sFullFileName = oFInfo.FullName;

sFileExtension = oFInfo.Extension;

lFileLength=oFInfo.Length;

sFilePath = oFInfo.Directory.ToString();

dCreationDate = oFInfo.CreationTime;

dModificationDate = oFInfo.LastWriteTime;

#region "read File Details"

aDetailedInfo = GetDetailedFileInfo(sFPath);

foreach(DetailedFileInfo oDFI in aDetailedInfo)

{

switch(oDFI.ID)

{

case 2:

sFileType = oDFI.Value;

break;

case 9:

sFileAuthor = oDFI.Value;

break;

case 10:

sFileTitle = oDFI.Value;

break;

case 11:

sFileSubject = oDFI.Value;

break;

case 12:

sFileCategory = oDFI.Value;

break;

case 14:

sFileComment = oDFI.Value;

break;

default:

break;

}

}

#endregion

}

else

{

throw new Exception("The given File does not exist");

}

}

#region "Properties"

public string FileName

{

get{return sFileName;}

set{sFileName=value;}

}

public string FilePath

{

get{return sFilePath;}

set{sFilePath = value;}

}

public string FullFileName

{

get{return sFullFileName;}

set{sFullFileName=value;}

}

public string FileExtension

{

get{return sFileExtension;}

set{sFileExtension=value;}

}

public long FileSize

{

get{return lFileLength;}

set{lFileLength=value;}

}

public long FileVersion

{

get{return lFileVersion;}

set{lFileVersion=value;}

}

public DateTime FileCreationDate

{

get{return dCreationDate;}

set{dCreationDate=value;}

}

public DateTime FileModificationDate

{

get{return dModificationDate;}

set{dModificationDate=value;}

}

public string FileType

{

get{return sFileType;}

}

public string FileTitle

{

get{return sFileTitle;}

}

public string FileSubject

{

get{return sFileSubject ;}

}

public string FileAuthor

{

get{return sFileAuthor ;}

}

public string FileCategory

{

get{return sFileCategory ;}

}

public string FileComment

{

get{return sFileComment ;}

}

#endregion

#region "Methods"

private ArrayList GetDetailedFileInfo(string sFile)

{

ArrayList aReturn = new ArrayList();

if(sFile.Length>0)

{

try

{

// Creating a ShellClass Object from the Shell32

ShellClass sh = new ShellClass();

// Creating a Folder Object from Folder that inculdes the File

Folder dir = sh.NameSpace( Path.GetDirectoryName( sFile ) );

// Creating a new FolderItem from Folder that includes the File

FolderItem item = dir.ParseName( Path.GetFileName( sFile ) );

// loop throw the Folder Items

for( int i = 0; i < 30; i++ )

{

// read the current detail Info from the FolderItem Object

//(Retrieves details about an item in a folder. For example, its size, type, or the time of its last modification.)

// some examples:

// 0 Retrieves the name of the item.

// 1 Retrieves the size of the item.

// 2 Retrieves the type of the item.

// 3 Retrieves the date and time that the item was last modified.

// 4 Retrieves the attributes of the item.

// -1 Retrieves the info tip information for the item.

string det = dir.GetDetailsOf( item, i );

// Create a helper Object for holding the current Information

// an put it into a ArrayList

DetailedFileInfo oFileInfo = new DetailedFileInfo(i,det);

aReturn.Add(oFileInfo);

}

}

catch(Exception)

{

}

}

return aReturn;

}

#endregion

}

// Helper Class from holding the detailed File Informations

// of the System

public class DetailedFileInfo

{

int iID = 0;

string sValue ="";

public int ID

{

get{return iID;}

set

{

iID=value;

}

}

public string Value

{

get{return sValue;}

set{sValue = value;}

}

public DetailedFileInfo(int ID, string Value)

{

iID = ID;

sValue = Value;

}

}

如何用C#获得文件信息以及扩展信息的更多相关文章

  1. 如何去掉drwxr-xr-x@中的@符号Linux文件扩展信息

    如何去掉drwxr-xr-x@中的@符号Linux文件扩展信息ls -lart drwxrwxrwx@ 10 rlanffy staff 340B 3 6 2015 files-rwxrwxrwx@ ...

  2. Windows Shell编程之如何编写为文件对象弹出信息框的Shell扩展

    有关COM编程资料 转载:http://www.cnblogs.com/lzjsky/archive/2010/11/22/1884702.html 活动桌面引入一项新特性, 当你在某些特定对象上旋停 ...

  3. C# 对包含文件或目录路径信息的 System.String 实例执行操作

    在字符串操作中有一类比较特殊的操作,就是对包含文件或目录路径信息的 System.String 实例执行操作.比如根据一个表示路径的字符串获取其代表的文件名称.文件夹路径.文件扩展名等.在很多时候,我 ...

  4. 使用dreamever去掉文件头部BOM(bom)信息 From 百度经验

    本文来此百度经验: 地址为:http://jingyan.baidu.com/article/3f16e003c3dc172591c103e6.html OM主要处理浏览器窗口与框架,但事实上,浏览器 ...

  5. 如何用原生js开发一个Chrome扩展程序

    原文地址:How to Build a Simple Chrome Extension in Vanilla JavaScript 开发一个Chrome扩展程序非常简单,只需要使用原生的js就可以完成 ...

  6. JPEG图片扩展信息读取与改动

    近日项目中须要用到往jpg图片中写入信息(非水印),经调研发现Android中已经封装了读写jpg图片扩展信息的api(ExifInterface). 相应api地址:http://developer ...

  7. 血淋淋的事实告诉你:你为什么不应该在JS文件中保存敏感信息

    在JavaScript文件中存储敏感数据,不仅是一种错误的实践方式,而且还是一种非常危险的行为,长期以来大家都知道这一点. 而原因也非常简单,我们可以假设你为你的用户动态生成了一个包含API密钥的Ja ...

  8. php文件上传错误信息

    错误信息说明 UPLOAD_ERR_OK:其值为0,没有错误发生,文件上传成功 UPLOAD_ERR_INI_SIZE:其值为1,上传的文件超过了php.ini和upload_max_filesize ...

  9. php命令行查看扩展信息

    通常,在php的开发过程中,我们会使用到第三方扩展,这时候,我们对于php扩展的信息的查看就显得尤为重要了.一般情况下,我们查看到扩展信息,都是直接通过 cat *.ini  文件来进行,这样子的效率 ...

随机推荐

  1. 12天学好C语言——记录我的C语言学习之路(Day 9)

    12天学好C语言--记录我的C语言学习之路 Day 9: 函数部分告一段落,但是我们并不是把函数完全放下,因为函数无处不在,我们今后的程序仍然会大量运用到函数 //转入指针部分的学习,了解指针是什么 ...

  2. C++实用数据结构:二叉索引树

    看下面这个问题(动态连续和查询): 有一个数组A(长度为n),要求进行两种操作: add(i,x):让Ai增大x: query(a,b):询问Aa+Aa+1+...+Ab的和: 若进行模拟,则每次qu ...

  3. js闭包的使用例子

    网上关于闭包的介绍太多,这就导致了泛滥,对于新手来说,网上好多讲解就说了闭包是啥,还都是用下面这种例子: 我的天啊,我们都看了不知道多少遍了,看完有啥用?在什么场合下用啊? 于是我翻阅各种资料,自己总 ...

  4. ifstream:incomplete type is not allowed

    IntelliSense: incomplete type is not allowed ifstream inputFile; Need to add this: #include <fstr ...

  5. hdu 5056 Boring count

    贪心算法.需要计算分别以每个字母结尾的且每个字母出现的次数不超过k的字符串,我们设定一个初始位置s,然后用游标i从头到尾遍历字符串,使用map记录期间各个字母出现的次数,如果以s开头i结尾的字符串满足 ...

  6. Linux – RedHat7 / CentOS 7 忘记root密码修改

    1.(a) 开机出现grub boot loader 开机选项菜单时,立即点击键盘任意鍵,boot loader 会暂停. (b) 按下’e’,编辑选项菜单(c) 移动上下鍵至linux16 核心命令 ...

  7. Qt-获取主机网络信息之QHostInfo

    Qt中提供了几个用于获取主机网络信息的类,包括QHostInfo.QHostAddress.QNetworkInterface以及QNetworkAddress.在本节中,我将在这里总结QHostIn ...

  8. cookie、localStorage、sessionStorage之间的区别

    sessionStorage 和 localStorage 是HTML5 Web Storage API 提供的,可以方便的在web请求之间保存数据.有了本地数据,就可以避免数据在浏览器和服务器间不必 ...

  9. AJAX安全-Session做Token

    个人思路,请大神看到了指点 个人理解token是防止扫号机或者恶意注册.恶意发表灌水,有些JS写的token算法,也会被抓出来被利用,个人感觉还是用会过期的Session做token更好,服务器存储, ...

  10. Aspose.Words 总结

    生成答题卡 try { string tempPath = @"D:\moban\ttt.doc"; //Open document and create Documentbuil ...