在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;
}
}
