1.取文件基本信息
首先需要根据文件名来创建FileInfo对象
FileInfo fi = new FileInfo( yourFileName );
那么以后就可以通过此对象来访问文件一些属性,例如文件大小,创建时间,最后访问时间,最后写入时间等等,还可以通过访问对象的Attributes属性,来获得当前文件是只读、隐藏之类属性.
2.取文件Version
并不是所有的文件都有Version信息,因此在使用FileVersionInfo的时候需要注意的是,最好先判断一下文件的扩展名。不过一个FileVersionInfo类对象不能通过构造函数来创建,需要调用类的静态方法来获得,例如:
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo( yourFileName );
通过此对象,可以获得文件的产品名称,公司名,版本号,语言版本,版权等等
3.取文件擴展信息,如标题,作者等等
.net框架中並沒有提供相關的類來支持這個操作,但我們可以利用Com組件"Microsoft Shell Controls and Automation"取得文件的擴展信息,这个COM是由系统“Shell32.dll”提供.實現步驟如下:
--1.首先寫一個幫助類
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;
}
}
--2.編寫取文件信息的類
using System.Collections;
using Shell32;
/// <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
}
4.一個具體的問題,如何判斷文件真正的格式.
簡單的通過文件後綴名,我們是得不到文件真正的格式的.文件真正的格式是通過文件頭來做判斷的.那麼我們該如何取得文件真正的格式呢?如果是圖片,那麼好辦,.通過.net自帶的函數就可以輕松判斷圖片真正的格式:
{
if ( ! File.Exists(filesName)) return false ;
bool isTrue = false ;
using (System.Drawing.Image img = System.Drawing.Image.FromFile(filesName))
{
if (img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Jpeg)) isTrue = true ;
}
return isTrue;
}
對於非圖片的文件,.net框架好像並沒有提供相關的支持,由於文件類型有很多,而且很多文件的文件頭格式並沒有公開,所以這其實是個相當複雜的問題.目前我還沒有找到很好的方法.以下幾個鏈接或許對認識這個問題有所幫助:
首先看看文件的類型吧:
http://www.utoronto.ca/webdocs/HTMLdocs/Book/Book-3ed/appb/mimetype.html
再來看看不同類型的文件頭:
http://blog.csdn.net/georgejin/archive/2007/08/11/1737927.aspx
最後看一下編程實現的2個例子:
http://www.cnblogs.com/jhtchina/archive/2007/02/12/93412.html
http://www.tcdongli.com/article.asp?id=14
參考文獻: http://blog.csdn.net/knight94/archive/2006/05/07/711327.aspx